-2

I've tried to create a regular expression to search only images that not contain a specified class, but the server gives me 0 error unexpected '/' separator.

$regex = '/<img((?!(.*?)class=['\"](.*?)nopopup(.*?)['\"](.*?)).*?>/';

Now my code is the next, I need that preg_match work only in images that not contain class nopopup

$regex      = '/<img.*?>/';

$createImageLink = function($imageTag)
{ 
preg_match('/src="([^"]*)"/i', $imageTag[0], $results);
$srcAttribute = $results[0];
$srcUrl = explode("=", $srcAttribute);
return '<a href=' . $srcUrl[1] . ' class="magnific_popup">'.$imageTag[0].'</a>';
};                                                     

if (isset($article->text))
{
$article->text = preg_replace_callback($regex, $createImageLink, $article->text);
$article->text .= '<script> jQuery( document ).ready(function() { jQuery(".magnific_popup").magnificPopup({ type: "image", enableEscapeKey:true }); });</script>';
}
if (isset($article->text))
{
$article->introtext = preg_replace_callback($regex, $createImageLink, $article->introtext);
$article->introtext .= '<script> jQuery( document ).ready(function() { jQuery(".magnific_popup").magnificPopup({ type: "image", enableEscapeKey:true }); });</script>';
}
break;

Where is the error?

  • 1
    What do you mean `0 error`? And how do you run the regex and what is sample input? – AbraCadaver Dec 20 '19 at 12:36
  • 1
    You should probably ditch regexes in favor of [DOM parsing](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php). – Jeto Dec 20 '19 at 12:36
  • Question is too unclear for me, you get `0 error` or `unexpected '/' separator`? The `$regex` is invalid syntax and would never parse so you can't be getting either error. You also never use `$regex`. What is `$imageTag`? Why not use a parser? – user3783243 Dec 20 '19 at 12:45
  • Where'd the regex go with negative lookaheads? – user3783243 Dec 20 '19 at 12:52

1 Answers1

0

You must do like this :

$regex = '/<img(?!(.*?)class=['\"](.*?)nopopup(.*?)['\"](.*?)).*?>/';

in first line you have excess ( symbol

https://regexr.com/4r4er

user3783243
  • 5,368
  • 5
  • 22
  • 41