0

I am unablt to use the following code to match the following string, I do not know what is going on

$regex = "/\\251/i";
if (preg_match($regex, $contents)) {
  echo 'good it works';
}

the $contents are as follows

Shanghai Taipei Tokyo TorontoOxford is a registered trade mark of Oxford University Pressin the UK and in certain other countriesPublished in the United Statesby Oxford University Press Inc., New YorkRevised text \251 John Guy 2000

I have tried encoding the string to no luck

$contents = utf8_encode ($contents);

this works in all the online tools i used, am I missing anything to see why it would not work when being run?

devanand
  • 5,116
  • 2
  • 20
  • 19

3 Answers3

0

In this case want to use the raw (unescaped) backslash to match the character 251 (Character octal 0251 is the ISO-LATIN-1 copyright symbol).

Try simply \251 to match the © character directly, since it looks like your content is already UTF8 encoded.

See an example: http://regex101.com/r/eT3mZ8

brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

You should try with:

preg_match('/(\xA9)/i', $text);

Or:

preg_match('/(\251)/i', $text);
Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
0

I tried in PERL to check whether regex is proper or not. Please find the following perl code which has regex which perfectly matches \251

#!C:\Perl64\bin
$regex = "Shanghai Taipei Tokyo TorontoOxford is a registered trade mark of Oxford University Pressin the UK and in certain other countriesPublished in the United Statesby Oxford University Press Inc., New YorkRevised text \251 John Guy 2000";
if ("$regex" =~ m/(\251)/)
{
    print "Success scenario\n";
    print "$regex is matched \n";
} else {
    print "$regex is not matched \n";
}

This will output -->

Success scenario
Shanghai Taipei Tokyo TorontoOxford is a registered trade mark of Oxford Univers

ity Pressin the UK and in certain other countriesPublished in the United Statesb y Oxford University Press Inc., New YorkRevised text \251 John Guy 2000 is matched

Hope this might help you

Thanks

Jigar
  • 300
  • 4
  • 13