SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get a html tag and store in a php variable

    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?

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whenever I need to dig through HTML in PHP I use this package:
    http://sourceforge.net/projects/simplehtmldom/
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <?php
    $txt = file_get_contents($url);
    
    $arr = get_tag($txt, "h1");
    
    print_r($arr);
    
    function get_tag($txt,$tag){
    	$offset = 0;
    	$start_tag = "<".$tag;
    	$end_tag = "</".$tag.">";
    	$arr = array();
    	do{
    	$pos = strpos($txt,$start_tag,$offset);	
    	if($pos){
    		$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;
    	}
    	}while($pos);
    	return $arr;
    
    }
    ?>
    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...
    cheers...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •