note 70052 modified in function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:31:12 +0000 |
| Subject: | note 70052 modified in function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155129@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 !
<?php
$body = 'this is a text with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
looooooooooooooooooooooooooooong word! <a href="http://www.example.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);
?>
--was--
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 !
<?php
$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);
?>
http://php.net/manual/en/function.wordwrap.php