SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extracting data using preg_match_all

    So I used file_get_contents to download a page. I extracted a chunk of data I want, but I want to get rid of the html around the value that I'm looking for.

    <title>Sewer Waste</title>

    How would I pull "Sewer Waste" out of that code using preg_match_all.

    PHP Code:
    if(preg_match_all"/<title>/^[a-zA-Z][\w\s]*/<\/title>/i"$card$gtdump))
                    {
                        
    $gt $gtdump[0][0];
                    }
                    else 
                    {
                        echo 
    'Cannot be retrieved.';
                    } 
    If I remove the <title></title> from the search, I will return many results. There must be a simpler way to extract just the text inbetween the <title> tag's?

  2. #2
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try using lookahead or lookbehind assertions

    http://us.php.net/manual/en/regexp.r...assertions.php

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    PHP Code:
    $content = @file_get_contents("http://www.DOMAIN.COM/");
    if (
    $content !== false)
    {
            
    $pattern '#<title>(.+?)</title>#si';
            if (
    preg_match($pattern$content$m))
            {
                    
    $title $m[1];
                    echo 
    $title;
            }


  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    or you could just strip_tags the result. or str_replace... or substr... or... or...


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
  •