2

I have this code that selects all <img> tags:

$pattern = "@<img[^>]*src=[\"\']([^\"\']*)[\"\'][^>]*>@";

I want to change it that only selects images that do not have "noajax" in its class. for example:

<img src="../" /> -> should be selected

<img src="../" class="noresize noajax" /> -> should NOT be selected

2
  • I'll add the standard comment that RegEx is the wrong tool to use to parse HTML. If your case is narrow and you're not dealing with arbitrary HTML, it might be 'good enough', but in the real world it will keep biting you. Commented Oct 21, 2014 at 12:24
  • @RikHeywood: forget it. All is lost. He comes. Commented Oct 21, 2014 at 12:28

2 Answers 2

1
<img[^>]*src=[\"\']([^\"\']*)[\"\'](?:(?!\bnoajax\b|>).)*>

Try this.This should work for you case.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to change it that only sellects all images that have not "noajax" in its class
0

@RikHeywood's comment is correct. I would recommend XPath as the preferred tool to solve this problem.

//img[not(contains(@class, 'noajax'))]

This should select every in your document that does not have any class with the text "noajax".

A quick bit of googling suggests this as a place to start for how to evaluate XPaths in PHP. http://php.net/manual/en/domxpath.evaluate.php

Edit: forgot link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.