note 23223 deleted from function.wordwrap by googleguy

From: Date: Tue, 22 Oct 2013 09:03:15 +0000
Subject: note 23223 deleted from function.wordwrap by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196860@lists.php.net to get a copy of this message
Note Submitter: toxic79_spam at yahoo dot com ---- I've noticed that if you have a string of, say, 8 characters and you set it to wrap at 8 characters with a break tag it will add the break tag after the 8th character even if the string is only 8 characters long. this was frustrating because I only wanted the break tag if there would be more text after it. it would be cool to have a flag that would stop this from happening, or a flag that would put the break tag (or whatever you use) before the number you break on, so you could set it to break every 9th character and it would only insert the tag after the 8th if there are more than 8 characters. until then, however, here is my lame fix. $str is the string to wrap, $num is the number of characters to wrap after and $break is the text to use to wrap the string. <?php function my_wordwrap( $str, $num, $break ) { // get the wordwrapped string $tmp_str = wordwrap( trim( $str ), $num, $break, 1 ); // get the string length of the breaking tag $strlen_break = strlen( $break ); // if the last $strlen_break characters of the wordwrapped string is $break ... if ( substr( $tmp_str, ( 0 - $strlen_break )) == $break ) { // strip the last $strlen_break characters off the end $tmp_str = substr( $tmp_str, 0, count( $tmp_str ) - $strlen_break - 1 ); } // return the results return $tmp_str; } ?>

« previous php.notes (#196860) next »