Marc Dupuis
Marc Dupuis

Reputation: 531

Extract img src from string with preg_match_all

I've been trying to use preg_match_all for 30 minutes but it looks like I can't do it.

Basically I have a $var which contains a string of HTML code. For example:

<br>iihfuhuf
<img title="Image: http://www.jlnv2.local/temp/temp513caca536fcd.jpeg"   
 src="http://www.jlnv2.local/temp/temp513caca536fcd.jpeg">
<img src="http://www.jlnv2.local/temp/temp513caca73b8da.jpeg"><br>

I want to get the src attribute values of img tags that contain /temp/temp[a-z0-9]{13}\.jpeg in their src value.

This is what I have so far:

preg_match_all('!(<img.*src=".*/temp/temp[a-z0-9]{13}\.jpeg"(.*alt=".*")?>)!', $content, $matches);

Upvotes: 1

Views: 18824

Answers (6)

Carrapicho
Carrapicho

Reputation: 122

$text = '<br>iihfuhuf<img title="Image: http://www.jlnv2.local/temp/temp513caca536fcd.jpeg" src="http://www.jlnv2.local/temp/temp513caca536fcd.jpeg"><img src="http://www.jlnv2.local/temp/temp513caca73b8da.jpeg"><br>';
$pattern = '#src="([^"]+/temp/temp[a-z0-9]{13}\.jpeg)"#';
    
preg_match( '@src="([^"]+)"@' , $text, $match );
$src = array_pop($match);

echo $src;

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88697

Here is a DOMDocument/DOMXPath based example of how to do it. This is arguably the only right way to do it, because unless you are really good at regular expressions there will most likely always be edge cases that will break your logic.

$doc = new DOMDocument;
$xpath = new DOMXPath($doc);

$doc->loadHTML($content);

$candidates = $xpath->query("//img[contains(@src, '/temp/temp')]");

$result = array();
foreach ($candidates as $image) {
  $src = $image->getAttribute('src');
  if (preg_match('/temp[0-9a-z]{13}\.jpeg$/', $src, $matches)) {
    $result[] = $src;
  }
}

print_r($result);

Upvotes: 0

Maks3w
Maks3w

Reputation: 6459

<img[^>]*src="([^"]*/temp/temp[a-z0-9]{13}\.jpeg)"

<img[^>]* Select IMG tags

src="([^"]*)" gets src value and save it as a match

/temp/temp[a-z0-9]{13}\.jpeg is the filter for src values

For quick RegEx tests use some online tool like http://regexpal.com/

Upvotes: 8

user1646111
user1646111

Reputation:

<?php
$text = '<br>iihfuhuf<img title="Image: http://www.jlnv2.local/temp/temp513caca536fcd.jpeg" src="http://www.jlnv2.local/temp/temp513caca536fcd.jpeg"><img src="http://www.jlnv2.local/temp/temp513caca73b8da.jpeg"><br>';
$pattern = '#src="([^"]+/temp/temp[a-z0-9]{13}\.jpeg)"#';
preg_match_all($pattern, $text, $out);
echo '<pre>';
print_r($out);
?>

Array
(
    [0] => Array
        (
            [0] => src="http://www.jlnv2.local/temp/temp513caca536fcd.jpeg"
            [1] => src="http://www.jlnv2.local/temp/temp513caca73b8da.jpeg"
        )

    [1] => Array
        (
            [0] => http://www.jlnv2.local/temp/temp513caca536fcd.jpeg
            [1] => http://www.jlnv2.local/temp/temp513caca73b8da.jpeg
        )

)

Upvotes: 0

Till Helge
Till Helge

Reputation: 9331

All you need to do is add another group to your regular expression. You have du surround everything you want to extract from the match with braces:

preg_match_all('!(<img.*src="(.*/temp/temp[a-z0-9]{13}\.jpeg)"(.*alt=".*")?>)!', $content, $matches);

You can see that working here. You can find the URLs in $matches[2].

But just for having said it: Regular expressions are no reasonable approach to extract anything from HTML. You would be better off using DOMDocument, XPath or something along that line.

Upvotes: 1

Emery King
Emery King

Reputation: 3534

Try this:

preg_match_all('/src="([^"]+temp[a-z0-9]{13}\.jpeg)"/',$url,$matches);

var_dump($matches);

Upvotes: 0

Related Questions