Skip to content Skip to sidebar Skip to footer

Extract Img Tag From A Html Code String Through Preg_match_all Php Function

I've some html code and extracted the img src attribute from it. Into the html string there are some img like this: ) as$image) { $images[] = $image->getAttribute('src'); }

Edit:

You are using the wpautop function to clean up the description. According to the documetation it requires the The text to be formatted. as first argument. So first make sure that it does preserve the image tags inside the argument.

As I assume that tags are preserved. Looking at the regex itself, I see that it's matching too little.

You are matching .*? inside the capuring group. The ? indicates to use lazy matching, which means match as few characters as needed. So .* will match any character, zero or more. And ? will match as few as needed.

In my ouptut of var_dump for $match I see that it found a match.

array (size=2)   0 => 
    array (size=1)
      0 => string'img src=' (length=8)   1 => 
    array (size=1)
      0 => string'' (length=0)

However the first matching group is of size 0. Because of the lazy matching. And I assume and internal php error. It should match everthing up to > because this is also part of the regex. But it seems php is ignoring this part.

If you change the capturing group to .+?, the first group will contain a single " character. Because of the + which means "one or more" characters.

A solution would be to change the code so it includes the quotation marks.

if (preg_match_all("<img src=\"(.*?)\">", $description, $match)) {

This matches the desired image link:

http://www.pecso.it/wp-content/uploads/2016/12/10_WRAS.png

I would recommend try using the DOMDocument approach as it's more likely this code will be more stable and extendable. If you want to learn about regex, parsing html might not be the best thing to start with.

All this code was tested using php 5.4, it might be diffrent for newer versions!

Post a Comment for "Extract Img Tag From A Html Code String Through Preg_match_all Php Function"