note 70052 modified in function.wordwrap by thiago

From: Date: Mon, 18 Aug 2008 17:27:36 +0000
Subject: note 70052 modified in function.wordwrap by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143602@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://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); ?> --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 ! $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

« previous php.notes (#143602) next »