note 105644 added to function.wordwrap
| From: | dev dot ivangc at gmail dot com | Date: | Sat, 03 Sep 2011 10:21:53 +0000 |
| Subject: | note 105644 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182916@lists.php.net to get a copy of this message | ||
This code extends wordwrap functionality to insert the <wbr /> tag inside a html text every 10
characteres per word without break it. The function works with utf-8 encoding:
<?php
function insertWbr($text) {
$pat = '/>([^<>]*?)([^\s<]{11,}?)(<|\z|\s)/';
return preg_replace_callback($pat, 'insertWbrCallback', $text);
}
function insertWbrCallback($matches){
$longWord = $matches[2];
$len = mb_strlen($longWord, 'UTF-8');
$lastBreak = 0;
$newString = '';
for($i=0; $i<$len; $i++){
if( ($i+1) % 10 == 0){
$newString .= mb_substr($longWord, $lastBreak, 10, 'UTF-8');
$newString .= '<wbr />';
$lastBreak = $i+1;
}
}
$newString .= mb_substr($longWord, $lastBreak, 10, 'UTF-8');
return '>'.$matches[1].$newString.$matches[3];
}
?>
How to use:
<?php
echo insertWbr('<p><a
href="example.com/foo/bar">example.com/foo/bar</a></p>');
?>
Output:
<p><a href="example.com/foo/bar">example.co<wbr
/>m/foo/bar</a></p>
----
Server IP: 80.64.47.13
Probable Submitter: 83.165.217.159
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/105644
Del: integrated -- https://master.php.net/note/delete/105644/integrated
Del: useless -- https://master.php.net/note/delete/105644/useless
Del: bad code -- https://master.php.net/note/delete/105644/bad+code
Del: spam -- https://master.php.net/note/delete/105644/spam
Del: non-english -- https://master.php.net/note/delete/105644/non-english
Del: in docs -- https://master.php.net/note/delete/105644/in+docs
Del: other reasons-- https://master.php.net/note/delete/105644
Reject -- https://master.php.net/note/reject/105644
Search -- https://master.php.net/manage/user-notes.php