PHP Help ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4Pics
    Confirmed User
    • Dec 2001
    • 7952

    #1

    PHP Help ...

    How do I grab a certain word from a webpage?

    For example

    PHP Code:
    $handle = fopen("http://www.blah.com", "rb");
    $contents = '';
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle); 
    
    That will assign the text to $contents, but then how do i find the text I want?

    I know its seperated uniquely on the page by a <span class="blah">textiwant</span>

    Please help

    Thanks
  • nestle
    Confirmed User
    • Apr 2006
    • 647

    #2
    Best to use regular expressions with the preg_match function:
    http://us2.php.net/manual/en/function.preg-match.php

    Comment

    • psili
      Confirmed User
      • Apr 2003
      • 5526

      #3
      lookup the function

      preg_match()


      and write a regular expression to grab your contents. Something like:

      $reg="/<.*>?(.*)<.*>/" or something like that.

      I haven't tried opening a url with "file_get_contents()" but you could try that instead of your loop, also.
      Your post count means nothing.

      Comment

      • Dirty D
        Confirmed User
        • May 2002
        • 4044

        #4
        This is what you want:

        $contents = str_replace("textiwant", "replacementtext", $contents);

        Dirty D - ICQ #1326843 - $1 Million Dollars of Bonus Money - 8,000+ FHG!
        Glory Hole Girlz - Crack Whore Confessions - Tampa Bukkake - Slut Wife Training - Fuck a Fan
        Electricity Play - Porn Video Drive - Theater Sluts - Skunk Riley - Ukraine Amateurs - Strapon Sessions

        Comment

        • pussyluver
          Clueless OleMan
          • Mar 2003
          • 11009

          #5
          Originally posted by 4Pics
          How do I grab a certain word from a webpage?

          For example

          PHP Code:
          $handle = fopen("http://www.blah.com", "rb");
          $contents = '';
          while (!feof($handle)) {
            $contents .= fread($handle, 8192);
          }
          fclose($handle); 
          
          That will assign the text to $contents, but then how do i find the text I want?

          I know its seperated uniquely on the page by a <span class="blah">textiwant</span>

          Please help

          Thanks
          WEB Police want to know what you are going to use this for???

          Comment

          • 4Pics
            Confirmed User
            • Dec 2001
            • 7952

            #6
            Originally posted by pussyluver
            WEB Police want to know what you are going to use this for???
            I want to grab scores from games and team names.

            Comment

            • testpie
              Mostly retired
              • Apr 2006
              • 3231

              #7
              If you are hosting with DreamHost (who disallow file(), file_get_contents() and similar using URL wrappers - so you have to use CURL), use:
              PHP Code:
              # Begin CURLing
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://www.blah.com");
                # Get content
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $content = curl_exec($ch);
              
                # Get word from regex string
                preg_match("'<span class=\"blah\">(.*?)</span>'i", $content, $word); 
              
              Otherwise (nearly all other webhosts) use:
              PHP Code:
              # Get content and load into string
               $content = file_get_contents("http://www.blah.com");
              
                # Get word from regex string
                preg_match("'<span class=\"blah\">(.*?)</span>'i", $content, $word); 
              
              In both instances the word you are looking for is returned in the variable $word.

              Hit me up on ICQ if you have any problems.

              Affiliates: DogFart ~ Domain parking: NameDrive ~ Traffic broker: Traffic Holder

              Comment

              • nestle
                Confirmed User
                • Apr 2006
                • 647

                #8
                Originally posted by dustman
                This is what you want:

                $contents = str_replace("textiwant", "replacementtext", $contents);
                I think you misunderstood. He does not know what 'textiwant' is nor does he want to replace it. He wants to find what 'textiwant' is.

                Comment

                • 4Pics
                  Confirmed User
                  • Dec 2001
                  • 7952

                  #9
                  Originally posted by testpie
                  If you are hosting with DreamHost (who disallow file(), file_get_contents() and similar using URL wrappers - so you have to use CURL), use:
                  PHP Code:
                  # Begin CURLing
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, "http://www.blah.com");
                    # Get content
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $content = curl_exec($ch);
                  
                    # Get word from regex string
                    preg_match("'<span class=\"blah\">(.*?)</span>'i", $content, $word); 
                  
                  Otherwise (nearly all other webhosts) use:
                  PHP Code:
                  # Get content and load into string
                   $content = file_get_contents("http://www.blah.com");
                  
                    # Get word from regex string
                    preg_match("'<span class=\"blah\">(.*?)</span>'i", $content, $word); 
                  
                  In both instances the word you are looking for is returned in the variable $word.

                  Hit me up on ICQ if you have any problems.

                  Thanks, this worked .. Now I just have to play around to get the rest of it.

                  Comment

                  • 2HousePlague
                    CURATOR
                    • Jul 2004
                    • 14572

                    #10
                    Originally posted by 4Pics
                    I want to grab scores from games and team names.




                    2hp
                    tada!

                    Comment

                    • gooddomains
                      Too lazy to set a custom title
                      • Jul 2003
                      • 10127

                      #11
                      some nice info

                      Comment

                      • testpie
                        Mostly retired
                        • Apr 2006
                        • 3231

                        #12
                        Originally posted by 4Pics
                        Thanks, this worked .. Now I just have to play around to get the rest of it.
                        Glad to have helped - now if you could just forward the hookers and booze to...

                        Affiliates: DogFart ~ Domain parking: NameDrive ~ Traffic broker: Traffic Holder

                        Comment

                        Working...