Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace 2

Status
Not open for further replies.

stasJohn

Programmer
May 6, 2004
155
US
I'm not sure if this is the place to ask... it is php related though.

I'm trying to come up with a regular expression. Here's the scenario. I'm pulling addresses from an xml file.
Code:
<addressList>
  <thisIsAnAddress>
  blah blah
  blah blah blah
  </thisIsAnAddress>
  ...
</addressList>

Using xsl, I transform it to...
Code:
<address>
blah blah
blah blah blah
</address>

Of course, when displayed on the screen, both lines appear as one.

So...
Code:
$output = [xsl transformation]
//convert "\n" in <address> tags to <br />
print $output

How do I, using a regular expression, find
"<address>[bunch of text]</address>"
and convert the "\n" to "<br />"s???

Thanks in advance!
 
aha Success!!!

Yeah, I realized it was stripping the address tags, but threw in some extra parenthesis to fix that.

Here's the final snippet...

Code:
$patterns = '/(<address>\n)([^<]+)\n\s*(<\/address>)/e';
$replacement = "'\\1' . nl2br('\\2') . '\n' . '\\3'";	
echo preg_replace($patterns,$replacement,$html);

Nice and elegant.

Thanks to the both of you for the help. Regular expressions are strange to get a handle on, but I think I'm starting to figure them out. Thanks again. Appreciate it!!
 
DRJ478,

Thanks, for tuning it up.

Runs much better and doesn't smoke as much.

-Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top