vaso123
vaso123

Reputation: 12391

Regex get all image when word is not in source

I have some description for our products, but sadly, this is a legacy code, and there is a lot of html elements, what I really do not need. I remove these with strip_tags but I need to keep images. Unfortunatly, the previous deisgn what is so old, contained strings after strip like this:

<IMG border=0 alt="" src="http:///Images/white.gif" width=5 height=1>
<IMG border=0 alt="" src="http:///Images/white.gif" width=170 height=1>

<img  src="/asdsa/alkatreszkepek/aasdsa.jpg">

<IMG border=0 alt="" src="http:///Images/white.gif" width=170 height=1>

<IMG border=0 alt="" src="http:///Images/white.gif" width=2 height=1>

I need to replace all the images, what not contains alkatreszkepek (means product picutres) in the src tags.

What I tried so far:

first get only one image:

<img.*?src=".*?".*?>

Cool, so I am using isg modifiers, it is found all images.

Now I am try to negate:

<img.*?src=".*?^(?!.alkatreszkepek).*?".*?>

but in this case nothing will be selected.

Can somebody help me, how te get all those unnecessary images?

I want to preg_replace them to nothing in PHP.

EDIT

Yes, I know, I could get all images with preg_match_all , then iterate through on the matches, and easily str_replace them I just want to know, how can I do it with regex.

Upvotes: 0

Views: 84

Answers (1)

freedev
freedev

Reputation: 30027

This should work (not very efficient though):

(?:<img[^>]+src\s*=\s*(["'])(((?!alkatreszkepek).)*?))\1[^>]*>

The regex above will match any img, not containing the (sub) string 'alkatreszkepek' inside src attribute (handling different quotes " or ' )

This version in group #2 matches the image url

Tested here: https://regex101.com/r/dE5vS7/8

Upvotes: 2

Related Questions