note 70052 deleted from function.wordwrap by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:59:19 +0000 |
| Subject: | note 70052 deleted from function.wordwrap by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197274@lists.php.net to get a copy of this message | ||
Note Submitter: feedback at realitymedias dot com
----
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);
?>