-2

Text:

<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_28300"

My regex:

/<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_(.*?)"/mis

Code:

$regex = '/<a href="'.$text.'"><img id="img_(.*?)"/mis';
preg_match($regex, $text, $C);

Error:

Warning: preg_match(): Unknown modifier '/'
Kubra
  • 13
  • 1
  • 3

4 Answers4

5

You have to escape the slashes (/) in the regex:

/<a href="http:\/\/from.ae\/cameras-photo\/digital-cameras\/samsung-es95-white"><img id="img_(.*?)"/mis

Because you used / as your delimiter, PHP assumes everything after the second / is a modifier. In this case, that's another /, which is not a valid modifier.

You can do the escaping with preg_quote, like this:

$text = '<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_28300"';
$regex = '/'.preg_quote('<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_', '/').'(.*?)"/mis';
var_dump($regex);
var_dump( preg_match($regex, $text) );

Demo

An even simpler solution, as pointed out in the comments, is just to use a different delimiter. I'm fond of ~, but any character will work:

$text = '<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_28300"';
$regex = '~<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_(.*?)"~mis';
var_dump($regex);
var_dump( preg_match($regex, $text) );

Demo

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • `$regex = ' – Kubra Dec 21 '14 at 14:41
  • 2
    @Kubra: It's better if you take the problem identification made here and use it to write your _own_ code solution. Stack Overflow is not a magic code generator that does your programming work for you. – Lightness Races in Orbit Dec 21 '14 at 14:42
  • @Kubra Please see my edit; you can do the escaping with [**`preg_quote`**](http://php.net/manual/en/function.preg-quote.php). – elixenide Dec 21 '14 at 14:45
  • now it has two hrefs. – Avinash Raj Dec 21 '14 at 14:45
  • @AvinashRaj Yeah, I'm not sure exactly what OP is going for, either. I have tried to clarify. – elixenide Dec 21 '14 at 14:48
  • 2
    Quoting is good but using a different delimiter is even better because it keeps the RE clean. However, +1 for mentioning about `preq_quote()` because sometimes one simply cannot avoid it (when the RE is produced using data from external sources, f.e.) – axiac Dec 21 '14 at 14:58
2

The easiest way is to use another delimiter than / which can be in regex.

Updated regex could be, for example this one, with ~ as a delimiter:

~<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_(.*?)"~mis
pavel
  • 26,538
  • 10
  • 45
  • 61
0

You could simply use \K with a different php delimiter.

$text = '<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_28300"';
preg_match('~<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_\K[^"]*~mis', $text, $out);
echo $out[0];

Output:

28300
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
$text = '<a href="http://from.ae/cameras-photo/digital-cameras/samsung-es95-white"><img id="img_28300"';

preg_match('#<img id="img_(.*?)"#', $text,$match);
print_r($match);
echo $match[1];
Buse Gönen
  • 238
  • 1
  • 5