theblackjacker — 2009-10-24T01:48:31-04:00 — #1
Hi I use the file_get_content to grab a webpage (url) and I want to
get the content in some specific tags. <h1> and <h2>
I have read DOM-document is the best way to do this but I'm just not sure
how to use it and most I find on google is for XML.. which won't help me I guess.
So how do I take the content in the <h1> tag from a webpage and store it in a php variable?
zarin_denatrose — 2009-10-24T08:46:37-04:00 — #2
Whenever I need to dig through HTML in PHP I use this package:
It makes it easy to load html from a file, url or string, then traverse it using CSS-like selectors. It comes with manual pages and examples, so it's all pretty simple.
bonyyousuf — 2009-10-27T07:05:17-04:00 — #3
i wrote a function for u just now... this will help u get <h1> or <h2> tag
this function will return an array containing the tags content
$txt = file_get_contents($url);
$arr = get_tag($txt, "h1");
$offset = 0;
$start_tag = "<".$tag;
$end_tag = "</".$tag.">";
$arr = array();
$pos = strpos($txt,$start_tag,$offset);
$str_pos = strpos($txt,">",$pos)+1;
$end_pos = strpos($txt,$end_tag,$str_pos);
$len = $end_pos - $str_pos;
$f_text = substr($txt,$str_pos,$len);
$arr = $f_text;
$offset = $end_pos;
ps: this function will only work for non nested tag.. a tag which contains the same tag as child within it will give u wrong result.. meaning div tag like tag which can contain a child div tag will give u wrong answer..
if u also need that sort of function which works on nested tag, let me know..
i will try to write that for u...