Strings

The strings utility functions provide tools for generating random strings and truncating text while preserving word integrity.

Available Functions

cbo_random_string($length)

Generates a random string of the specified length.

Parameters:

  • $length: Length of the random string (default: 10).

Return Value:

Returns a random string of the specified length.

1
// Generate a random string of length 10
2
$random_string = cbo_random_string();
3
echo $random_string; // e.g., "a3b2c7d9e8"
4
5
// Generate a random string of length 16
6
$random_string = cbo_random_string(16);
7
echo $random_string; // e.g., "a3b2c7d9e8f1g6h5"
8
9
// Use as a unique identifier
10
$unique_id = 'element-' . cbo_random_string(8);
11
echo '<div id="' . $unique_id . '">Content</div>';

cbo_truncate($text, $chars)

Truncates a string to the specified length while preserving word integrity.

Parameters:

  • $text: The text to truncate.
  • $chars: Maximum number of characters (default: 200).

Return Value:

Returns the truncated text with ellipsis (...) appended if truncation occurred.

1
// Truncate text to 50 characters
2
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
3
$truncated = cbo_truncate($text, 50);
4
echo $truncated; // "Lorem ipsum dolor sit amet, consectetur adipiscing..."
5
6
// Truncate a post excerpt
7
function custom_excerpt($post_id, $length = 150) {
8
$post = get_post($post_id);
9
$content = $post->post_content;
10
11
// Strip shortcodes
12
$content = strip_shortcodes($content);
13
14
// Strip HTML tags
15
$content = strip_tags($content);
16
17
// Truncate
18
return cbo_truncate($content, $length);
19
}
20
21
// Usage
22
$excerpt = custom_excerpt(123, 100);
23
echo $excerpt;

Advanced Usage Examples

Generating Unique Tokens

1
// Generate a unique token for password reset
2
function generate_password_reset_token($user_id) {
3
$token = cbo_random_string(32);
4
5
// Store the token in user meta with expiration
6
$expiration = time() + 24 * HOUR_IN_SECONDS; // 24 hours
7
update_user_meta($user_id, 'password_reset_token', $token);
8
update_user_meta($user_id, 'password_reset_expiration', $expiration);
9
10
return $token;
11
}
12
13
// Verify a password reset token
14
function verify_password_reset_token($user_id, $token) {
15
$stored_token = get_user_meta($user_id, 'password_reset_token', true);
16
$expiration = get_user_meta($user_id, 'password_reset_expiration', true);
17
18
if (empty($stored_token) || empty($expiration)) {
19
return false;
20
}
21
22
if (time() > $expiration) {
23
// Token has expired
24
delete_user_meta($user_id, 'password_reset_token');
25
delete_user_meta($user_id, 'password_reset_expiration');
26
return false;
27
}
28
29
return $token === $stored_token;
30
}
31
32
// Usage
33
$user_id = 123;
34
$token = generate_password_reset_token($user_id);
35
$reset_url = add_query_arg(['user_id' => $user_id, 'token' => $token], wp_login_url());
36
37
echo 'Password reset link: ' . $reset_url;

Creating Post Previews

1
// Create previews for a list of posts
2
function display_post_previews($posts, $excerpt_length = 150) {
3
if (empty($posts)) {
4
return;
5
}
6
7
echo '<div class="post-previews">';
8
9
foreach ($posts as $post) {
10
// Get post data
11
$title = get_the_title($post);
12
$permalink = get_permalink($post);
13
$content = get_the_content(null, false, $post);
14
$content = strip_shortcodes($content);
15
$content = strip_tags($content);
16
17
// Truncate content
18
$excerpt = cbo_truncate($content, $excerpt_length);
19
20
// Display post preview
21
echo '<div class="post-preview">';
22
echo '<h3><a href="' . $permalink . '">' . $title . '</a></h3>';
23
echo '<div class="excerpt">' . $excerpt . '</div>';
24
echo '<a href="' . $permalink . '" class="read-more">Read more</a>';
25
echo '</div>';
26
}
27
28
echo '</div>';
29
}
30
31
// Usage
32
$recent_posts = get_posts(['numberposts' => 5]);
33
display_post_previews($recent_posts, 120);

Best Practices

  • Use cbo_random_string for generating unique identifiers, but not for security-critical purposes like passwords.
  • Use cbo_truncate instead of PHP's substr for text that should be truncated at word boundaries.
  • Consider the context when choosing the truncation length to ensure readability.
  • Strip HTML tags before truncating content to avoid broken HTML in the output.