Forum Moderators: coopster

Message Too Old, No Replies

PHP Error

preg_match_all

         

kujoe

6:09 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



i'm getting error:

preg_match_all(): Unknown modifier '/' blahblahblah eval()'d code on line **

for the following code:

preg_match_all("/http:\/\/scd.mm-[0-9a-z]*.yimg.com\/image\/[0-9]*/", $source, $result);

i've trade changeing to this:

preg_match_all("/http:\/\/[0-9a-z]*.mm-[0-9a-z]*.yimg.com\/image\/[0-9]*", $source, $result);

or

preg_match_all("/http:\/\/[\w]*.mm-[\w]*.yimg.com\/image\/[\d]*", $source, $result);

but still get an error.

any suggesstions?

gliff

6:48 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



I executed your first line of PHP code in a file by itself and didn't get any errors, so it might be another part of your code.

Also, I don't know if this is anything, but you have your regular expression declared inside double quotes. I've always avoided doing that, as it was unclear to me if the regular expression engine would see the following

preg_match("/\\n/")

as a newline, or as a literal \n, since strings require escaping and then the regular expression engine require escaping. I always use single quotes instead, giving me one less thing to have to think about during debugging.

preg_match('/\n/')

Hope that helps.

ergophobe

5:15 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I get confused too, so like gliff, I use single quotes. When you use double quotes, the string effectively gets evaluated one extra time. That's not quite true as in either case, a backslash escapes a backslash.

So

echo 'You are in \/usr\/bin';
// output: You are in /usr/bin;

echo 'You are in /usr/bin';
// output: You are in /usr/bin;

YOU WANT

echo 'You are in \\/usr\\/bin';
// output: You are in \/usr\/bin;

because you want the \/ sequence to get sent to the regex engine

There is, however, and easier way around the whole thing. You can use most any delimiter you want with the preg* functions, so here's a good time to use something like a back tick or a hash mark or whatever so you don't have to escape the slashes at all

$pattern = '`/http://scd.mm-[0-9a-z]*.yimg.com/image/[0-9]*`';
$pattern = '#/http://scd.mm-[0-9a-z]*.yimg.com/image/[0-9]*#';