note 64045 deleted from function.wordwrap by bjori
| From: | bjori@php.net | Date: | Thu, 06 Apr 2006 16:37:41 +0000 |
| Subject: | note 64045 deleted from function.wordwrap by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107448@lists.php.net to get a copy of this message | ||
Note Submitter: rayRO
Reason: bad code
----
Hope this helps if u want to cut long words in a text, having html or smthing in it.
<?php
function cutLongWords($str, $len = false) {
if ($len == false && isset($GLOBALS['cutLongWordLenght']))
$len = $GLOBALS['cutLongWordLenght'];
else
$GLOBALS['cutLongWordLenght'] = $len = ($len != 24) ? $len : 24;
if (is_array($str))
return $str[1].' '.cutLongWords($str[2], $len);
return preg_replace_callback(
"#([\w\d-]{".$len."})([\w\d-]*?)(?!(-|\d|\w|\/|\.))#iUs",
'cutLongWords', $str);
}
// testing
$text = 'put your looooooooooooooo0000000000000ooo0000oong text here';
$new = cutLongWords($text);
print('<pre>');
print($text."<br />");
print($new);
print('</pre>');
$new2 = cutLongWords($text, 12);
print('<pre>');
print($text."<br />");
print($new2);
print('</pre>');
?>
This is some solution using and explaining preg_replace callbacks. Dunno if it's buggy... Not
testet...