2 Replies - 288 Views - Last Post: 05 February 2018 - 07:47 AM Rate Topic: -----

#1 luckie12   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 237
  • Joined: 27-February 10

preg_match find full div

Posted 05 February 2018 - 07:27 AM

Hello :)/>

So i am new to preg_match.

I want to make a php code to fetch a website (done that with cURL) and get a specific div, with all the code inbetween.

So at the moment i have:

preg_match_all('/<img [a-zA-Z0-9 -_]*src="(.*)"/U', $data, $title);


and i get all the <img> tags. But i need to 'scan' the website on a logo tag, because i want to fetch everything that contains the word 'logo'. Like, when a website has for example:


<div class='logo-holder'>
    <img src="logo.png" class="logo">
</div>



I basicly want that full block back, instead of only the IMG tag. But i have no idea how to adapt my preg_match code to that part...

Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: preg_match find full div

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4212
  • View blog
  • Posts: 13,305
  • Joined: 08-June 10

Re: preg_match find full div

Posted 05 February 2018 - 07:46 AM

Regular Expression are ill-suited for parsing HTML. Better use PHP's DOMDocument or SimpleXML extensions, which allow fine-grained searches.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,724
  • Joined: 12-December 12

Re: preg_match find full div

Posted 05 February 2018 - 07:47 AM

You should use an HTML parser to read and explore HTML, not regex.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1