Search/Matching Code

Discussion in 'PHP' started by Christian Little, Feb 29, 2008.

  1. #1
    Here's what I need - to provide a URL and a chunk of HTML to a function, and have it return a boolean if it finds the HTML on the specific page.

    So: checklinks($url, $html)

    Where:
    $url = "http://www.example.com/pagexyz.html";
    $html = "<a href=\"http://www.example.com\">Example Link</a> - This is sample description text to go with my link.";

    Basically I'm building a script to check if people are keeping their reciprocal links to my sites.

    I appreciate the help :)
     
    Christian Little, Feb 29, 2008 IP
  2. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use the file() function to suck the page into a var.

    Then use strpos() or a preg function to check if it is there.
    You may also want to check if it is nofollow or not ;)

    That is the quick and dirty method. I would use cURL instead and XML DOM functions to test all <a> tags.
    That will prevent your code being fooled or throwing false negatives/positives.
    Slightly more complicated but works best in my experience.

    There are tonnes of scripts around to do what you are looking for. Are you doing this as an exercise to learn or what?
     
    RoscoeT, Feb 29, 2008 IP
  3. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function checklinks($url, $html) {
    	$data = file_get_contents($url);
    	if(eregi($html, $data))
    	{
    		return "Match";
    	}
    	else
    	{
    		return "No Match";
    	}
    }
    $url = "http://www.example.com/pagexyz.html";
    $html = "<a href=\"http://www.example.com\">Example Link</a> - This is sample description text to go with my link.";
    echo checklinks($url, $html);
    
    PHP:
    That should do the job.
     
    Vbot, Feb 29, 2008 IP
  4. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the help. I'm building a small SEO script that I'm going to use for some clients. In the past I've done most of this manually or bought expensive software. Either way it's a pain and I want to have certain custom features, so I'm building my own from scratch but I have limited php knowledge (i.e. I've hacked wordpress and made a few simple apps, but that's about it), so I'm learning as I go :)
     
    Christian Little, Mar 1, 2008 IP