preg_replace() and wordwrap()

Hey Everyone!

I’m having a bit of a problem here! Basically, what I want to do, is take the string data from out of a pair of XML tags like so:

<Details>This is the string data I want  to manipulate</Details>

I want to do a preg_replace to find the contents of the tags, and then wordwrap it, using wordwrap() to 50 chars, with the br / tag. However, the text will NOT wrap, and my breaks, no matter which I use, will NOT be inserted.

Here is my current code:

[code] /* Attempt to wrap text in the

tag */

		$in_str = "/<Details>(.*?)</Details>/si"; // Looks for whatever is between the tags
		$replace = "<Details>" . wordwrap("$1", 50, "<br />", FALSE) . "</Details>"; 

		$data = preg_replace($in_str, $replace, $data); [/code]

I’ve played around with a few different ways to phrase this same basic idea, and none have worked… I guess I’m looking for an obvious over-sight or even a totally different, alternate way. Just so long as I can wrap long lines inside of a set of tags, I’ll be happy!

Thanks in advance!

I think you’re making things a little complicated on yourself by trying to mix everything together into one statement.

  1. Filter out the string to be changed into a separate variable
  2. Wordwrap() the value of the separate variable
  3. Use a simple str_replace() to replace each occurrance of the string in the data.

ha!!

Thank you very much!! You were exactly right. Here is what I ended up doing, for anyone else looking to do something similar:

/*---------------------------------------------------------
Wrap long lines inside of an HTML/XML tag
---------------------------------------------------------*/
$in_str = "/<Details>(.*?)</Details>/si";                                             // The Perl compatible Regex we're using to 
$lookup = preg_match($in_str, $data, $out_str);                                            // Find the tags, and put the contents into the var $replace_str
$replace_str = wordwrap($out_str[0], 125, "<br />rn", FALSE);                            //Do the wordwrap to 125 lines, using <br /> and rn line breaks
$data = str_replace($out_str[0], "<Details>" . $replace_str . "</Details>", $data);       // Replace the text between the tags with our modified, wrapped text
Sponsor our Newsletter | Privacy Policy | Terms of Service