Question

i have string (not html tag)

e.g :

<div class="lh">
    <b>Review: <b>Sarah Geronimo</b> leaves UAE fans asking for more</b></a><br>
    <font size="-1"><b><font color="#6f6f6f">gulfnews.com</font></b></font><br>
    <font size="-1"><b>Geronimo</b> how to get this contents <b>...</b></font><br>
    <font size="-1" class="p"></font><br><font class="p" size="-1"><nobr><b>and more&nbsp;»</b></nobr></a></font>
</div>

i want get strings "how to get this contents" . how i do that with regex ?

UPDATE

in this case why do I use regex? because results it contains html tags

I'm parsing this site https://news.google.com/news/feeds?q=sarah%20geronimo%20&output=rss

this all my code :

$html = new simple_html_dom();
$html = file_get_html("https://news.google.com/news/feeds?q=sarah%20geronimo%20&output=rss");

foreach($html->find('item') as $item) {
    $items['desc'] = $item->find('description',0)->plaintext;   
    $data[] = $items;
}

$regex = '~(?s)<div[^>]*>(?:.*?<font size){2}[^>]*><b>.*?</b>\K[^<]+~';

foreach($data as $content) {
        $desc = $content['desc'];
        preg_match($regex,$desc ,$m);
        echo $m[0];
}

i want get content in tag <description>. which the content containing html tag and i'm using regex to remove it

but its return blank ?

Was it helpful?

Solution

With this regex:

(?s)<div[^>]*>(?:.*?<font size){2}[^>]*><b>.*?</b>\K[^<]+

How to use it:

$str = '<div class="lh">
    <b>Review: <b>Sarah Geronimo</b> leaves UAE fans asking for more</b></a><br>
    <font size="-1"><b><font color="#6f6f6f">gulfnews.com</font></b></font><br>
    <font size="-1"><b>Geronimo</b> how to get this contents <b>...</b></font><br>
    <font size="-1" class="p"></font><br><font class="p" size="-1"><nobr><b>and more&nbsp;»</b></nobr></a></font>
</div>';

$regex = '~(?s)<div[^>]*>(?:.*?<font size){2}[^>]*><b>.*?</b>\K[^<]+~';

if(preg_match($regex,$str,$m)) {
    echo $m[0]."<br />";
}

Output:

how to get this contents 

Let me know if you have any questions. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top