Preg Match

Discussion in 'PHP' started by bigrollerdave, Jan 25, 2008.

  1. #1
    For some reason coding usually comes easy to me, but I can't for the life of me figure out preg's. I should take some time and study it because it's very useful. Anyways here is a simple script I found to check a back link on another site. I want to modify this code to check not only for a back link but also a rel="nofollow" tag. How would I edit this code to do that?

    Thanks

    function check_back_link($remote_url, $your_link) {
        $match_pattern = preg_quote(rtrim($your_link, "/"), "/");
        $found = false;
    	$nofollow = false;
        if ($handle = @fopen($remote_url, "r")) {
            while (!feof($handle)) {
                $part = fread($handle, 1024);
                if (preg_match("/<a(.*)href=[\"']".$match_pattern.
    "(\/?)[\"'](.*)>(.*)<\/a>/", $part)) {
                    $found = true;
                    break;
                }
            }
            fclose($handle);
        }
        return $found;
    }
    
    PHP:

     
    bigrollerdave, Jan 25, 2008 IP
  2. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    function check_back_link($remote_url, $your_link) {
        $match_pattern = preg_quote(rtrim($your_link, "/"), "/");
        $found = false;
        $nofollow = false;
        if ($handle = @fopen($remote_url, "r")) {
            while (!feof($handle)) {
                $part = fread($handle, 1024);
                if (preg_match("/<a(.*)href=[\"']".$match_pattern.
    "(\/?)[\"'](.*)>(.*)<\/a>/", $part, $match)) {
    				print_r($match);
    				if (preg_match("/rel=[\"']?nofollow[\"']?/", $match[0]))
    					$found = false;
    				else
    	                $found = true;
                    break;
                }
            }
            fclose($handle);
        }
        return $found;
    }
    
    PHP:
     
    kreoton, Jan 25, 2008 IP
    bigrollerdave likes this.
  3. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks a lot that works perfectly
     
    bigrollerdave, Jan 25, 2008 IP