0

I would like to write some PHP code to parse some HTML to return the href and title of all of the images in the "image_url" a class. I am currently using substr to find the location of the strings, however the webpage sometimes changes content and I can't always rely on substr. Is there a DOM way or some other regular expression way I can find these elements within my HTML?

<a class="image_url" href="/123.jpg" target="_blank"><img src="/123.jpg" border="0" title="Some dynamic length image title that is making this difficult for me"></a>
4

2 Answers 2

0

You can use the 3 things as far as I know

  1. php simple xml parsing
  2. php domDoucment.
  3. Simplehtmldom parser
Sign up to request clarification or add additional context in comments.

Comments

0

You can try below sample code.

$html='<a class="image_url" href="/123.jpg" target="_blank"><img src="/123.jpg" border="0" title="Some dynamic length image title that is making this difficult for me"></a>';   
 $dom = new DOMDocument();
    $dom->loadHTML($html);

    $elements = $dom->getElementsByTagName('a');
    foreach ($elements as $child) {
        echo $child->nodeValue;
    }

Comments

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.