Php Help needed...

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

    #1

    Php Help needed...

    Can someone share some code that will grab from a webpage all a href's and img's on the page? Also how to grab a certain text string out of a webpage.

    Thanks
  • Alky
    Confirmed User
    • Apr 2002
    • 5651

    #2
    http://www.php.net/manual/en/function.preg-grep.php

    Comment

    • Why
      MFBA
      • Mar 2003
      • 7230

      #3
      php.net is where all questions about php are answered. much better place to seek answers then GFY.

      Comment

      • fletcher
        Confirmed User
        • Jan 2003
        • 698

        #4
        I'll be nice and share one regexp

        This grabs all linked images on a page:

        PHP Code:
        /<a.*href=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><img.*src=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><\/a>/im 
        
        In PHP, do it like this:

        PHP Code:
        preg_match_all("/<a.*href=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><img.*src=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><\/a>/im", $string, $matches) 
        
        $matches is an array with all of the images with links. Just change up the regexp for plain links, images, etc.
        &nbsp;
        [email protected]
        ICQ: 6411138

        Comment

        • swedguy
          Confirmed User
          • Jan 2002
          • 7981

          #5
          http://zend.com/codex.php?id=231&single=1

          Comment

          • 4Pics
            Confirmed User
            • Dec 2001
            • 7952

            #6
            Originally posted by fletcher
            I'll be nice and share one regexp

            This grabs all linked images on a page:

            PHP Code:
            /<a.*href=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><img.*src=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><\/a>/im 
            
            In PHP, do it like this:

            PHP Code:
            preg_match_all("/<a.*href=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><img.*src=[\"|\']([^\"|^\'|^\s]+)[\"|\'].*><\/a>/im", $string, $matches) 
            
            $matches is an array with all of the images with links. Just change up the regexp for plain links, images, etc.
            Thanks, but is your top link correct? It looks like it is cutoff?

            Comment

            Working...