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 !!!!
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'
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);