note 81144 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:07:41 +0000 |
| Subject: | note 81144 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137434@lists.php.net to get a copy of this message | ||
Note Submitter: sean at nojunkplz . boyercentral .net
----
I wanted to word wrap the text inside of an XML tag for a project I'm working on. This function
will wrap the text inside of any kind of angle-bracket tags, and can be modified to use other tag
types (ie. bbcode)
function wrap_tags($tag, $string, $limit = '200', $break = "<br />\r\n"){
/*------------------------------------------------------
wrap_tags()
by Sean Boyer - February 15, 2008
This function will wrap the text that's inside of a
set of HTML or XML tags.
Useage:
$tag - name of the tag you want to to wrap the
contents of.
brackets!
$string - the string that the tag is
contained in
$limit - the nearest number of chars before
the line wraps (ie. this will NOT cut a word
in the middle to put half on one line, half
on the next
default is 200 chars
$break - the type of line break to use
default is "<br />\r\n"
------------------------------------------------------*/
// Just incase the angle brackets are were included...
$tag = str_replace("<","",$tag);
$tag = str_replace("</","",$tag);
$tag = str_replace(">","",$tag);
$in_str = "/\<$tag\>(.*?)\<\/$tag\>/si";
$lookup = preg_match($in_str, $string, $out_str);
$replace_str = wordwrap($out_str[0], $limit, $break, FALSE);
$string = str_replace($out_str[0], "<$tag>" . $replace_str .
"</$tag>", $string);
return $string;
}