0

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?

2
Contributors
11
Replies
13
Views
4 Years
Discussion Span
Last Post by rakibtg
Featured Replies
  • 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 … Read More

  • $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](http://simplehtmldom.sourceforge.net/) :) Read More

0

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";
0

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?

0

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?

1

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);
0

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

0

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.

0

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?

Edited by rakibtg

2
$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 :)

Edited by blocblue

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.