Jump to content

Help... Checking if DOM textContent matches row thats in database.


Recommended Posts

Hello,

 

I am trying to get this backlink checker that checks to make sure another website has my link on their website, so far I can check to make sure the "href" link is there and is correct, but I cant get the textContent to prove valid or not it seems to always be proving true even when the textConent is not in the mysql database.

 

The other persons website will have this:

<a href="http://businessinvestingsorce.com">Business Investing Source</a>

 

I need to make sure Business Investing Source (the textContent) matches the anchor in the database.

 

Here is my code:

 

<?php

function CheckForAnchorMatch($anchortext) {    

$query = mysql_query("SELECT * FROM allowed_anchor WHERE anchor='$anchortext'");
	if(mysql_num_rows($query) != 0) {
		return true;
	}

return false;

} 

function CheckForTargetUrl($links, $target) {    
    foreach ($links as $link) {
        if (strpos($link, $target) !== false) {
            return true;
        }
    }

    return false;
} 
                          
function ExtractHrefLinks($html) {
    $dom = new DOMDocument;
    $linkUrls = array();
     

    @$dom->loadHTML($html);
    $links = $dom->getElementsByTagName('a');
     
    foreach ($links as $link){
        $linkUrls[] = $link->getAttribute('href');
    }
    
    return $linkUrls;
}

function ExtractAnchorText($html) {
    $dom = new DOMDocument;
    $AnchorTexts = array();
     

    @$dom->loadHTML($html);
    $links = $dom->getElementsByTagName('a');
     
    foreach ($links as $link){
        $AnchorTexts[] = $link->textContent;
    }
    return $AnchorTexts;
}

function backlink_check($contenturl){

$url = 'businessinvestingsource.com';
$source = $contenturl;

$falseFound = false;

    $html = file_get_contents ( $source );
    $links = ExtractHrefLinks($html);

    if (CheckForTargetUrl($links, $url) === false) {
        $falseFound = "true";
        $reportArray[$source] = 0;
    } else {
        $falseFound = "false";
    }


    $html = file_get_contents ( $source );
    $anchortext = ExtractAnchorText($html);
    
    if (CheckForAnchorMatch($anchortext) === false) {
        $falseFound = true;
    } else {
        $falseFound = false;
    }

return $falseFound;
}
?>

 

Help greatly appreciated

that's not the best coding, also, do you have your targets sign off on the fact that you will be ripping all the link information from their page just to check if your link is there properly?

 

what do you get if you return and print_r($links) from ExtractAnchorText()? is it populated properly?

Well your idea makes sense but the problem is I am trying to make sure the user puts a valid backlink that has the proper href and innerhtml I dont want them to just put the link and leave the text blank so its a false link.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.