note 70052 added to function.wordwrap
| From: | feedbackatrealitymediasdotcom at osu1 dot php dot net | Date: | Sun, 01 Oct 2006 18:50:55 +0000 |
| Subject: | note 70052 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117907@lists.php.net to get a copy of this message | ||
Here is a way to use wordwrap to add spaces to long "words", without breaking link tags --
I came up with this as there apparently is no way to reproduce this effect with a regular
expression.
This is mostly used in users-posts where your output layout could be broken with words that are
longer than what the container object can take - it will not effect html tags (such as <a
href="aaaaaaaaaaaaaaaaaaaaaaaaa"> so you don't end up with broken links !
$body = 'this is a text with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
looooooooooooooooooooooooooooong word! <a href="http://ww.this.site.com/with/a/loooooooooooooooooooooong/url'>
... </a>';
foreach(explode(" ", strip_tags($body)) as $key => $line) {
if (strlen($line) > 35) $body = str_replace($line, wordwrap($line, 25, " ", 1),
$body);
}
(In case you would like the same thing without the tags protection, there is a regular expression
way;
$body = preg_replace("/([^\s]{35})/","$1 ",$body);
----
Server IP: 216.235.15.211
Probable Submitter: 70.80.9.195
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/70052
Del: integrated -- https://master.php.net/note/delete/70052/integrated
Del: useless -- https://master.php.net/note/delete/70052/useless
Del: bad code -- https://master.php.net/note/delete/70052/bad+code
Del: spam -- https://master.php.net/note/delete/70052/spam
Del: non-english -- https://master.php.net/note/delete/70052/non-english
Del: in docs -- https://master.php.net/note/delete/70052/in+docs
Del: other reasons-- https://master.php.net/note/delete/70052
Reject -- https://master.php.net/note/reject/70052
Search -- https://master.php.net/manage/user-notes.php