My PHP script can fetch content from a div id, but what is the way to filter this fetch data and exclude some of its content which has this div id <div id="navbar" class="n">content here content here</div> I have tried with this code but its not working

$regex = '#\<div id="navbar"\>(.+?)\<\/div\>#s'; preg_match($regex, $displaybody, $matches); $match = $matches[0]; echo "$match";

To fetch content i am using SIMPLE HTML DOM Parser( http://sourceforge.net/projects/simplehtmldom/files/ ), any idea?

Recommended Answers

All 11 Replies

Try changing your regex pattern to:
'/<div id="navbar"[^>]+>([^<]*)<\/div>/s'

thanks for that but my mistake i can't make it works :( here what i am doing, any idea?

$html = file_get_html("http://www.example.com");
$displaybody = $html->find('div[id=res]', 0);
$regex = '/<div id="navbar"[^>]+>([^<]*)<\/div>/s';
preg_match($regex, $displaybody, $matches);
$match = $matches[0];
echo "$match";

What exactly are you trying to accomplish? You say exclude some of it's content. Do you mean exclude? extract?

The regex I wrote works with the source you included in your original post. What exactly is in $displaybody??

What's the content of $matches?

ok, with this script i will fetch content from a website, it will fetch only contents from a div id called res suppose i will have this filtered content in a variable like $displaybody. now again $displaybody will be filtered by deleting content from a div id called navbar and then it will announced in a new variable.. is it possible?

Can you post the content of $displaybody and $matches so I can see what the source code looks like and what is or isn't being matched??

If you just want to remove the whole of the navbar, can you not just use str_replace, rather than preg_replace / preg_match??

Perhaps something like (I've not used the library before, so am guessing at how it might work).

$html = file_get_html('http://www.example.com');
$result = $html->find('div[id=res]', 0);
$nav_bar = $html->find('div[id=navbar]', 0);

$result = str_replace($nav_bar, '', $result);
$result = str_get_html($result);

thanks @blocblue it works :)

one more question there is another TAG i want to remove i dont know what it is, it's look like this <nobr>content</nobr> so $result will also remove all content of <nobr>content</nobr> is it possible? Thanks

Is it the only occurence in the source code?
If not, is it always the first, second, third,... occurence that you want to remove?

Once you can answer those questions, do the same as for the navbar. Find the source, and replace it with an empty string.

well, it's working but there is <nobr></nobr> tag for almost 10 time, and this will delete only the first one and the others <nobr></nobr> content was there still. Here is what i have tried:

$result = $html->find('div[id=res]', 0);
$nav_bar = $html->find('div[id=navbar]', 0);
$result = str_replace($nav_bar, '', $result);
$result = str_get_html($result);
$nobr = $html->find('nobr', 0);
$result = str_replace($nobr, '', $result);
$result = str_get_html($result);
echo "$result";

what to do to delete all <nobr></nobr> available on this content?

$result = $html->find('div[id=res]', 0);
$nav_bar = $html->find('div[id=navbar]', 0);
$result = str_replace($nav_bar, '', $result);
$result = str_get_html($result);

foreach($html->find('nobr') as $nobr)
    $result = str_replace($nobr, '', $result);

$result = str_get_html($result);

$result = str_get_html($result);
echo "$result";

Tip for the future... RTFM :)

Thats all i need to know, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.