Jump to content

Preg_replace


son
 Share

Recommended Posts

I try to replace certain strings/tags with preg_replace. My list is:

$patterns[0] = '/head_/';$patterns[1] = '/_head/';$patterns[2] = '/strong_/';$patterns[3] = '/_strong/';$patterns[4] = '/linkTarget_/';$patterns[5] = '/_linkTarget/';$patterns[6] = '/_linkClose/';$patterns[7] = '/custList_/';$patterns[8] = '/custListItem_/';$patterns[9] = '/_custListItem/';$patterns[10] = '/_custList/';$patterns[11] = '/£/';$patterns[12] = '/’/';$patterns[13] = '/“/';$patterns[14] = '/”/';$patterns[15] = '/–/';$patterns[16] = '</li><br />';$replacements[16] = '<h2>';$replacements[15] = '</h2>';$replacements[14] = '<strong>';$replacements[13] = '</strong>';$replacements[12] = '<a href="';$replacements[11] = '" title="Please follow link">';$replacements[10] = '</a>';$replacements[9] = '<ul class="listingBullet">';$replacements[8] = '<li>';$replacements[7] = '</li>';$replacements[6] = '</ul>';$replacements[5] = '£';$replacements[4] = "'";$replacements[3] = '"';$replacements[2] = '"';$replacements[1] = '-';$replacements[0] = '</li>';

Pattern 16 does not only not work, but makes also all text from this point in code disappear. Where am I going wrong?Son

Link to comment
Share on other sites

It's the fact that slashes are one of the reserved characters for regular expressions, so you have to escape them:$patterns[16] = '<\/li><br \/>';
Hi Ingolme,Great! Now I start receiving results;-)What is if i want to modifiy pattern 13$patterns[13] = '/<br \/><br \/>/';and change only if there is a new line in between those two <br />s? Also how do you replace $replacements[2] = '</p><p>';so there is a new line in between those two tags? You make me very happy if you help me with those two as well;-) Otherwise, I might get a serious headache....Son
Link to comment
Share on other sites

The answer to both of your questions is \n, it tells the program to interpret a new line:$patterns[13] = '/<br \/>\n<br \/>/';$replacements[2] = '</p>\n<p>';
I tried it, but for example for the second replacement it shows:\non screen. And:</p>\n<p>in source code. Do you know why this is?Son
Link to comment
Share on other sites

Well, since you're working in PHP, you can just put a line break right there, PHP allows it.

$replacements[2] = '</p><p>';

I'm not sure why PHP isn't parsing the line break, but this method shouldn't fail.Edit: In fact, it might be because the string is surrounded in single quotes.Try double-quotes:$replacements[2] = "</p>\n<p>";

Link to comment
Share on other sites

The double-quotes make all the difference. Have$replacements[0] = "</p>\n<p>";working now. Cheers.There is just one more problem left. Having some <br /> tags on two lines they do not get replaced by the <br /><br /> pattern. I have a separate pattern as$patterns[13] = "/<br \/>\n<br \/>/";but this does not work.Do you know why? Also tried:$patterns[13] = "/<br \/><br \/>/";Son

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...