views:

24

answers:

1
preg_match(”[\\]“, $somedatawithbackslash);
throws exception: Warning: preg_match() [function.preg-match]: No ending matching delimiter \’]\’ found

preg_match(’/\\\\/’, $somedatawithbackslash);
throws exception: Warning: preg_match() [function.preg-match]: No ending matching delimiter \’]\’ found

preg_match('#\\#, $somedatawithbackslash);
throws exception: Warning: preg_match() [function.preg-match]: No ending delimiter \'#\' found

php...is so uerfriendly...pft

help?

Here is a test case:

<?php

$data = "<html xml:lang=\"en-US\"";

if (preg_match('/\\\\/', $data))
{
    $data = preg_replace('/\\\\/', '', $data, -1, $count);
    if ($count < 1)
    {
        print 'failed';
    }

    print "Replaced $count times: result: $data";
}
else
{
    print 'failed to match anything';
}

?>

+2  A: 
preg_match('#\\#', $somedatawithbackslash)

This regex is #\#. The \ is escaping the delimiter #, so it's not interpreted as a delimiter. Ditto for the first case.

The second one is correct (it matches \):

preg_match('/\\\\/', $somedatawithbackslash);

You want to match \. This character has a special meaning in regular expressions so you escape it. You get \\. However, \ also has a special meaning in PHP single quote strings. In particular '\\' gives the string \. So you have to escape again, and then you have \\\\.

Maybe the problem is that you're using "smart quotes" (like ” and ’) instead of the plain ones: ' and ".

Regarding the edit

In the snippet you posted, \ is not part of the data. It's just escaping the " character so it's interpreted literally instead of finishing the string literal.

Artefacto
Where would I be using these? Sorry, I have never heard of smart quotes
syn4k
@syn See the edit. There's no `\` in the string, that's why the regex doesn't match anything.
Artefacto
Yeah, nothing works...Thanks anyway
syn4k