0

i am trying to get specific HTML code portion with regex preg_match_all by matching it with class tag But it is returning empty array.

This is the html portion which i want to get from complete HTML

<div class="details">
    <div class="title">
        <a href="citation.cfm?id=2892225&CFID=598850954&CFTOKEN=15595705"   
        target="_self">Restrictification of function arguments</a>  
    </div>
</div>

Where I am using this regex

preg_match_all('~<div class=\'details\'>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $html, $matches );

NOTE: $html variable is having the whole html from which I want to search.

Thanks.

4
  • Maybe it's because you used the wrong quotes at \'details\' (-> \"details\" or ["']details["']) Commented Apr 9, 2016 at 19:04
  • What should the output be (in your example)? Commented Apr 9, 2016 at 19:06
  • using regex for the first time, i assume it should give the content inside the <div>. I don't know exactly how it will return the output. Commented Apr 9, 2016 at 19:13
  • inside the "title" div or the "details" div? Commented Apr 9, 2016 at 19:36

2 Answers 2

1

You are looking for single quotes in your regex in contrast to the double quotes in $html.

Your regex should look like:

'~<div class="details">\s*(<div.*?</div>\s*)?(.*?)</div>~is'

or better:

'~<div class=[\'"]details[\'"]>\s*(<div.*?</div>\s*)?(.*?)</div>~is'
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the answer. i am still getting empty array. I used as you told: preg_match_all('~<div class=[\'"]details[\'"]>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $html, $matches ); Am i doing something wrong?
I tried the test case you've shown and $matches isn't empty over here.
can you please please post full code you're trying? (or i could share mine)
Please update your question to show more code, as I'm not doing anything different than you're doing (as shown by now).
Your code is working. I was using html_entities function which was removing tags (yes i am terrible at this). Thank you very much
1

Better use a DOM approach !

<?php
$html = '<div class="details">
    <div class="title">
        <a href="citation.cfm?id=2892225&CFID=598850954&CFTOKEN=15595705"   
        target="_self">Restrictification of function arguments</a>  
    </div>
</div>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);
$divs = $xpath->query('//div[@class="title"]');
print_r($divs);
?>

4 Comments

Which is definitely the better approach than doing the regex.
Not sure that printing a DOMNodeList object will give the attempted result.
thanks but its for my class work, i have to use it with regex
@CasimiretHippolyte: It will certainly not, just to make sure any element has been found.

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.