0

How do I get all images path from a string? Note I just want the path containing the word "media".

For example given this string (part of the DOM)

<div class="my-class">
   <img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
   <h2 class="uk-margin-top-remove">About us</h2>                
</div>

I want an array containing a similar result:

array(
  [0] => "http://my-website.com/cache/media/2017/10/img67.png"
  [1] => "http://my-website.com/cache/media/2017/10/img69.png"
);

I don't want the second img because src attribute doesn't contain the word "media".

7
  • please state your current attempt Commented Jan 25, 2018 at 18:56
  • You have been a member long enough to know you should include code. Commented Jan 25, 2018 at 18:56
  • Dig into DOMDocument object, and try something out. Learning is best by attempting! You'll also stumble across all kinds of fun new things while you try. ... and you have way too many questions without accepted answers. Commented Jan 25, 2018 at 18:57
  • 2
    Don't use regex for this. Use a parser: H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ Commented Jan 25, 2018 at 18:58
  • Doing a simple search like. php get src from img gives you lots of answers. Then you can easily tailor it to your issue. FYI regex is not ideal here. DOMDocument() is right Commented Jan 25, 2018 at 18:59

2 Answers 2

2

You could use preg_match_all() to get URLs but it is even better to use a DOM reader.

$str = '<div class="my-class">
   <img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
   <h2 class="uk-margin-top-remove">About us</h2>                
</div>' ;

$matches = [] ;
preg_match_all('~(http\://my-website\.com/cache/media/(.*?))"~i', $str, $matches) ;
var_dump($matches[1]);

Will returns :

array(2) {
  [0]=>
  string(52) "http://my-website.com/cache/media/2017/10/img67.jpeg"
  [1]=>
  string(51) "http://my-website.com/cache/media/2017/10/img69.jpg"
}
Sign up to request clarification or add additional context in comments.

Comments

1

Some boilerplate code to get you started:

<?php

$data = <<<DATA
<div class="my-class">
   <img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
   <img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
   <h2 class="uk-margin-top-remove">About us</h2>                
</div>
DATA;

# set up the dom
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);# | LIBXML_COMPACT | LIBXML_NOENT );

# set up the xpath
$xpath = new DOMXPath($dom);

foreach ($xpath->query("//img[contains(@src, '/media/')]/@src") as $image) {
    echo $image->nodeValue . "\n";
}

Which yields

http://my-website.com/cache/media/2017/10/img67.jpeg
http://my-website.com/cache/media/2017/10/img69.jpg


This loads the DOM and uses an xpath query for every image where we'll loop over afterwards.
If for some reasons (why?) you are unable to use a DOM parser, your could use the secondbest option:

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

And use the src group, see a demo on regex101.com.

4 Comments

I was waiting for the Jan parser answer. User only wants URLs containing media though
@ctwheels: Updated thanks. I might get a bit calmer once I got the golden regex badge (only two to go!)
++ posted very similar then realized your answer :)
I deleted it as that looks like a dup answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.