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 102$random_string = cbo_random_string();3echo $random_string; // e.g., "a3b2c7d9e8"45// Generate a random string of length 166$random_string = cbo_random_string(16);7echo $random_string; // e.g., "a3b2c7d9e8f1g6h5"89// Use as a unique identifier10$unique_id = 'element-' . cbo_random_string(8);11echo '<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 characters2$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);4echo $truncated; // "Lorem ipsum dolor sit amet, consectetur adipiscing..."56// Truncate a post excerpt7function custom_excerpt($post_id, $length = 150) {8$post = get_post($post_id);9$content = $post->post_content;1011// Strip shortcodes12$content = strip_shortcodes($content);1314// Strip HTML tags15$content = strip_tags($content);1617// Truncate18return cbo_truncate($content, $length);19}2021// Usage22$excerpt = custom_excerpt(123, 100);23echo $excerpt;
Advanced Usage Examples
Generating Unique Tokens
1// Generate a unique token for password reset2function generate_password_reset_token($user_id) {3$token = cbo_random_string(32);45// Store the token in user meta with expiration6$expiration = time() + 24 * HOUR_IN_SECONDS; // 24 hours7update_user_meta($user_id, 'password_reset_token', $token);8update_user_meta($user_id, 'password_reset_expiration', $expiration);910return $token;11}1213// Verify a password reset token14function 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);1718if (empty($stored_token) || empty($expiration)) {19return false;20}2122if (time() > $expiration) {23// Token has expired24delete_user_meta($user_id, 'password_reset_token');25delete_user_meta($user_id, 'password_reset_expiration');26return false;27}2829return $token === $stored_token;30}3132// Usage33$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());3637echo 'Password reset link: ' . $reset_url;
Creating Post Previews
1// Create previews for a list of posts2function display_post_previews($posts, $excerpt_length = 150) {3if (empty($posts)) {4return;5}67echo '<div class="post-previews">';89foreach ($posts as $post) {10// Get post data11$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);1617// Truncate content18$excerpt = cbo_truncate($content, $excerpt_length);1920// Display post preview21echo '<div class="post-preview">';22echo '<h3><a href="' . $permalink . '">' . $title . '</a></h3>';23echo '<div class="excerpt">' . $excerpt . '</div>';24echo '<a href="' . $permalink . '" class="read-more">Read more</a>';25echo '</div>';26}2728echo '</div>';29}3031// Usage32$recent_posts = get_posts(['numberposts' => 5]);33display_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.