0

I'm building a regexp to get urls in img src attribute :

$string = 'Ut quo totam aperiam possimus recusandae
<img src="mylink.jpg"/>
Bo nostrum vero distinctio eligendi.
<img src="mylinktmpfileandsomeotherstuff.jpg"/>
<img src="myotherlink.jpg"/>
Lorem ipsum';

preg_match_all('/(?<=src=").+?(?=")/', $string, $matches);

var_dump($matches);

Which is giving :

array (size=1)
  0 => 
    array (size=3)
      0 => string 'mylink.jpg' (length=10)
      1 => string 'mylinktmpfileandsomeotherstuff.jpg' (length=34)
      2 => string 'myotherlink.jpg' (length=15)

https://regex101.com/r/UA3lVf/1

How can I filter my search to only get urls who contains the word "tmpfile" in it ?

So I would get :

array (size=1)
  0 => 
    array (size=1)
      0 => string 'mylinktmpfileandsomeotherstuff.jpg' (length=34)
Cephou
  • 264
  • 2
  • 15

1 Answers1

2

You don't need a regex to get the src of an image that contains tmpfile.

You can use an xpath query, with DomDocument and DOMXpath.

//img[contains(@src, 'tmpfile')]

For example:

$string = 'Ut quo totam aperiam possimus recusandae
<img src="mylink.jpg"/>
Bo nostrum vero distinctio eligendi.
<img src="mylinktmpfileandsomeotherstuff.jpg"/>
<img src="myotherlink.jpg"/>
Lorem ipsum';

$dom = new DomDocument();
$dom->loadHTML($string);
$xpath = new DOMXpath($dom);
foreach($xpath->query("//img[contains(@src, 'tmpfile')]") as $elm) {
    echo $elm->getAttribute("src") . PHP_EOL;
}

Output

mylinktmpfileandsomeotherstuff.jpg

Php demo

Martin
  • 19,815
  • 6
  • 53
  • 104
The fourth bird
  • 96,715
  • 14
  • 35
  • 52