Search code examples
phppreg-matchsimple-html-dom

Extract inner text from a div by id using an html string


i have an html string with the following divs only:

<div id="title">My Title</div>
<div id="image">http://www.mpahmplakdjfe.co.uk/images/01.jpg</div>
<div id="fullcontent">In this div there are some html elements more</div>

I need to extract the inner text from divs "My title" etc.

how is it possible to do this with preg_match?

I tried the following (simple html dom) without luck:

$html = new simple_html_dom();
$html->load_file($myhtml);
$ret = $html->find('div[id=title]')->innertext; (or outter) 
echo $ret;

Thanks !!!!


Solution

  • preg_match('|<[^>]*title[^>]*>(.*?)<|', $html, $m);
    

    will give you "My Title".

    preg_match('|<[^>]*image[^>]*>(.*?)<|', $html, $m);
    

    will give you "http//www.mpahmplakdjfe.co.uk/images/01.jpg".

    preg_match('|<[^>]*fullcontent[^>]*>(.*?)<|', $html, $m);
    

    will give you "some text here".

    You can do it that way:

    $html = '<div id="title">My Title</div>
    <div id="image">http://www.mpahmplakdjfe.co.uk/images/01.jpg</div>
    <div id="fullcontent">some text here</div>';
    
    $m = array();
    preg_match('|<[^>]*title[^>]*>(.*?)<|', $html, $m);
    // inner text is in $m[1]
    echo $m[1]; // == 'My Title'
    


    If you want to get all inner text from the string, use preg_match_all() instead of preg_match():

    // say you have that string
    $html = '<div id="fullcontent"><div>hi</div><div>hello</div></div>';
    
    $m = array();
    preg_match_all('|>(?<innerText>[^<]*)<|', $html, $m);
    echo count($m['innerText']); // 2     ;how many matches
    echo $m['innerText'][0];     // == 'hi'
    echo $m['innerText'][1];     // == 'hello'
    

    phpfiddle - http://x.co/6lbC6


    If you absolutely want inner texts only from <div>s, then you can modify preg_match_all() above like this:

    preg_match_all('|<div[^>]*>(?<innerText>[^<]+)<|', $html, $m);