0

I have this content:

<h1 class="contf _48_black caphead">
    <span>The essence of social media is knowing your audience and engaging them in something they love. In real-time, relevance and resonance!&nbsp;</span>
    <a href="https://twitter.com/" target="_blank">
        <img class="retwt" src="images/retweet.svg">
    </a>
    <span class="secmov"><img src="images/arrow.svg"></span>
</h1>

I am trying to extract the content from the h1 TAG as follows but not able to get it.

<?php
$result = <h1 class="contf _48_black caphead">
        <span>The essence of social media is knowing your audience and engaging them in something they love. In real-time, relevance and resonance!&nbsp;</span>
        <a href="https://twitter.com/" target="_blank">
            <img class="retwt" src="images/retweet.svg">
        </a>
        <span class="secmov"><img src="images/arrow.svg"></span>
    </h1>;

preg_match_all('$<h1(.*?)</h1>$i',$result, $subresult);
print_r($subresult);

?>

where as the following works-

preg_match_all('$span>(.*?)</span>$i',$result, $subresult);

Can some one please let me.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
VMN
  • 181
  • 1
  • 2
  • 14

2 Answers2

0

You have to use the m-option to make the dot match newlines.

preg_match_all('`<h1(.*?)</h1>`im',$result, $subresult);

But I agree with the comments that you should use DOMDocument instead.

flec
  • 2,891
  • 1
  • 22
  • 30
  • I won't use `$` as delimiter since it has a meaning in regex which might confuse further readers... – HamZa Dec 08 '14 at 12:26
  • @HamZa you are right. Usually I do not use them either. I was just lazy when copying the code. – flec Dec 08 '14 at 13:12
-1

Please check the reg_exp what i used below. I hope this what exactly you are looking for.

preg_match_all('/<h1.*?>(.*)<\/h1>/msi',$result, $subresult);
print_r($subresult);
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Using `.*?` and `.*` in the same expression with `U` is not only confusing but also misleading. I don't recommend the `U` modifier at all. – HamZa Dec 08 '14 at 12:29
  • Ok i have removed U :P – Vinoth Babu Dec 08 '14 at 12:31
  • It seems you might need to read [the difference between greedy and ungreedy/lazy quantifiers](http://stackoverflow.com/a/3075532). – HamZa Dec 08 '14 at 12:33
  • 1
    Thanks it worked. Only Modification I did was preg_match_all('/(.*?)<\/h1>/msi',$result, $subresult); Because when there were multiple h1 tags the results were all clubbed together.. – VMN Dec 08 '14 at 18:09