karacsi_maci karacsi_maci - 1 year ago 67
PHP Question

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

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
(means product picutres) in the src tags.

What I tried so far:

first get only one image:


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

Now I am try to negate:


but in this case nothing will be selected.

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

I want to
them to nothing in PHP.


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

Answer Source

This should work (not very efficient though):


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download