0

I'm making an automated script with PHP to check if my link exists at my partner website ( link exchange) .. besides making sure my link exists in the source code , I want to make sure he is not placing it in a HTML comment like <!-- http://www.mywebsite.com --> and cheating me ..

I tried to match it with REGEXP , but have failed

Naughty.Coder
  • 3,922
  • 7
  • 32
  • 41

2 Answers2

3

Use the DOM and XPath, it ignores comments:

$doc = new DOMDocument();
$doc->loadHTML($htmlstring);

$xpath = new DOMXPath($doc);

$result = $xpath->query('//a[contains(@href, "mywebsite.com")]');

if (!$result->length) echo "You've been cheated\n";

And then if you still want to know if your website is being commented out

if (strpos($htmlstring, 'mywebsite.com') !== false && !$result->length)
   echo "Your partner is hiding your link in a comment, sneaky bastard\n";
MooGoo
  • 46,796
  • 4
  • 39
  • 32
1

Sounds like a perfect use for an HTML parser like DOMDocument->loadHTML() and look for an anchor tag with your link. He could still remove it via javascript on the browser side, but that's a different issue.

If it's a cat and mouse game of "are you showing a link to my site" using a standard parser is your best bet. There are just too many ways for a regex to fail on html.

Community
  • 1
  • 1
Chadwick
  • 12,555
  • 7
  • 49
  • 66