HTML parser (Parse page titles)

Discussion in 'PHP' started by tsoft, Jan 14, 2009.

  1. #1
    Hi there DPers,

    Need some help with parsing HTML documents using PHP.

    I have tried this one: (regexp)

      if (eregi("<title>(.*)</title>", $val, $title)) {
                $page_title = $title[1];
                $is_title = true;
            } 
    Code (markup):
    but it's not working when parsing titles such as this one:

    <title>
           Some Page Title Here
    </title>
    Code (markup):
    By not working I mean that I cannot get the text "Some Page Title here".

    The parser breaks the HTML down line by line.
    Any other approach to this?
    Any help would be much appreciated.
     
    tsoft, Jan 14, 2009 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    
     if (preg_match_all("#<title>(.*?)</title>#si", $val, $title)) {
                $page_title = $title[1][0];
                $is_title = true;
            }
    
    PHP:
    Does this one work?
     
    elias_sorensen, Jan 14, 2009 IP