Help please for preg_replace();

Hello,
I am an old programmer but new to PHP. Have traced an errant PHP operation to the following lines:
$string="[IN TRADE] Servicemans essential test equipment.
My review: http://youtu.be/OiOipql_Zcc Saves you time and avoid blanket replace of electrolytic capacitor on faulty equipment when repairing TV, LCD, Audio board, etc. It tells you which electrolytics are out-of-spec IN CIRCUIT (or out)";

//after the following preg_replace the $string is sometimes NULL; why?
$string = preg_replace(’#(<[^>]+[\x00-\x20"’/])(on|xmlns)[^>]*>#iUu’, “$1>”, $string);

Of course I have checked the PHP help for preg_replace, but Id appreciate a breakdown on how the user bits of preg_replace work so I can then figure out why that step is inconsistent:
1…’#(<[^>]+[\x00-\x20"’/])(on|xmlns)[^>]*>#iUu’
2…"$1>"

Or, pointer in right direction.
Thanks vm. Alistair.

What you’re looking for is tutorials for REGEX in PHP, their’s lots of them. Just Google the search term.

I always use myregextester.com

It’s an easy way to test your Regex to make sure it’s going to do what you expect it to do.

[quote author=Topcoder
I always use myregextester.com
It’s an easy way to test your Regex to make sure it’s going to do what you expect it to do.
[/quote]
Wow, that myregestester.com is the berries. I was just wondering if you could give me a heads up on the ‘$1>’
I understand ‘$1’ would give me whats in the first set of brackets, but whats the greater sign do?
Thanks vm, Alistair.

I think you answered your own question…

“$1>” I still cant figure out what this does as well as
'# Where there is no mention any reference regex of what the # does or stands for

Thanks.

the > adds two “>>” on the end of whatever is in the first backgroup used as a replacement

is just a literal

So in order for you string to match it has to start with a # and end with a #Uu

That’s my understanding, but you should test and regex varies between languages.

$string = preg_replace(’#(<[^>]+[\x00-\x20"’/])(on|xmlns)[^>]*>#iUu’, “$1>”, $string);

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject)

Getting somewhere thanks but the end iUu mean insensitive, ungreedy, UTF-8
Using regex tools I cant get any matches for $subject though.

Regex is very hard for me. I always struggle with it. but I think you got your answer from…

Thanks but I did not know about experts-exchange (but now have joined) I see the question posed there was by another who has had the same problem as myself.
Temporarily, I have had to test for an empty string and replace it with original as a sticking-plaster fix for a defective regex test.

Sponsor our Newsletter | Privacy Policy | Terms of Service