...

View Full Version : preg_replace



Digicoder
12-30-2007, 03:27 AM
I'm having trouble with this and I don't see why.
I visited countless numbers of manuals on this and they don't explain anything I need.


$mytxt = preg_replace('/<p style="text-align: center;">(.+?)<\/p>/is', '\\1', $mytxt);

I'm trying to get it to turn this bit of html into a custom bbcode, well the thing is whenever my text gets run through, I come out with absolutely nothing.
No text nothing.
I don't understand it, I've escaped what I needed to before this and still got the same results, so as a ditch effort I've done this in java and it looks exactly like this and still works fine, whats the problem with my PHP here?

I know that < and > have meaning but if i add '\' that meaning is changed... and adding '\\' to it only escapes the first '\' making no difference at all...
I really need some help here.

Thanks any one who can help.

Inigoesdr
12-30-2007, 03:41 AM
That code works fine:

$mytxt = <<<END
<p style="text-align: center;">test1</p>
<p style="text-align: center;">test2</p>
<p style="text-align: center;">test3</p>
<p style="text-align: center;">test4</p>
END;

$mytxt = preg_replace('/<p style="text-align: center;">(.+?)<\/p>/is', '\\1', $mytxt);

echo $mytxt;
Outputs:

test1
test2
test3
test4

Output the code right before you use it to make sure you aren't modifying the code beforehand, and make sure it's being passed to preg_replace() how you're expecting it to.

Digicoder
12-30-2007, 03:58 AM
I really really don't understand this, perhaps I need some air...

This or one of the others like it have to be the problem yet they are all the same.

At any rate, thank you for your help, I will be sure to take your advice.

oesxyl
12-30-2007, 04:05 AM
I really really don't understand this, perhaps I need some air...

This or one of the others like it have to be the problem yet they are all the same.

At any rate, thank you for your help, I will be sure to take your advice.

check to see in your code, not in this post, if spelling for $mytxt or whatever is same in both parts of the preg_replace

more clear, the mistake could be:



$othervar = preg_replace(..., ..., $thisvar);


You can't see the difference if you test $thisvar, before and after preg_replace.

best regards

Digicoder
12-30-2007, 04:32 AM
Thanks for the advice all.

After some time away, I did locate the problem. I had used the wrong syntax for one of the regex's.

A simple overlooked slash can make all the difference. :rolleyes:
I should break more often, working like this can't be too good for a person.

thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum