preg_match question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I try to use preg_match to find out if a string begins with either "http",
"/somethime" or "../something". In my php script I have the following line:

if (0 == preg_match("/^(http.+)|(\/[a-z].+/)|(\.\.[a-z]+)/i",$string)

But I get a:

        Warning: Unknown modifier ')' in myscript.php at line nn

What's wrong with the pattern?

Jørn Dahl-Stamnes

Re: preg_match question

Jørn Dahl-Stamnes wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

If that is your exact line of code, then you missed escaping a slash
(trailing slash in second matching group, after the plus sign):

... |(\/[a-z].+/)| ...

So, when parser reaches that slash, it believes the regex itself is now
complete and tries to interpret everything after it as modifiers. Since
you have closed brace after the unescaped slash, and it is not a valid
regex modifier, it gives you the "unknown modifier" error.

If you find yourself working with regular expressions a lot, or if you
find them hard to understand or to work with, you may want to try
something like Regex Coach ( /), or a similar
tool. It helps me a lot, especially when things get complicated.

Re: preg_match question

malatestapunk wrote:

Quoted text here. Click to load it

Yes, I found that extra / in the expression some time after I posted my
message (typical :-)


Jørn Dahl-Stamnes

Site Timeline