3

I'm looking for a quick code/function that will detect if a page contains a certain thing.

It's for a new project I'm working on.

Basically, the user will paste a simple javascript code into their pages, but I need to make sure they do.

I need a code that will scan through a specific webpage url and find the code I provided.

Thanks!

4 Answers4

2

You need to do the 2 things:

1) get the content of remote url

2) check if the content contains your string:

if ( stristr($content, 'your_desired_string') )
{
    echo ' Yes, found';
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237
2

You can get the contents of a URL as a string, and search the contents for that code:

<?php
function check_url($url) {
    $page = file_get_contents($url);
    $code = '<script src="http://example.com/test.js"></script>';
    if (strpos($page, $code) === FALSE) {
        return false;
    } else {
        return true;
    }
}
?>

You may want to swap that simple strpos out for a regular expression, but this will do the trick.

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
2

You want to scan through a webpage, not an URL! You get to the webpage through an URL. :)

<?php
$contents = file_get_contents("http://some.site/page.html");
$search   = <<<EOF
<script type="text/javascript">
alert('They must have this!');
</script>
EOF;

if (strpos($contents, $search) === FALSE) {
    echo "Naughty webpage!";
}
?>

Note, though, that programmatically skimming pages like this is generally considered bad form.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Won't whitespace throw this off? – drudge Mar 15 '11 at 00:13
  • @jnpcl: How so? If you're looking at whitespace at the end of the `$search = <<` line, then [no](http://codepad.org/lyzkAs8O). – Lightness Races in Orbit Mar 15 '11 at 00:17
  • @Tomalak: A few spaces before `alert()` or the closing `` tag? – drudge Mar 15 '11 at 00:18
  • 1
    This is easily thrown of by html indentation or CRLF variations. Better to strip all whitespace from the $subject and $search text beforehand. – mario Mar 15 '11 at 00:22
  • @jnpcl: The OP wanted to search for some text on a webpage. If there are spaces on the webpage that aren't in the search string then, sure, it'll fail. It's not a match. If the requirements are more complex than stated and some dynamic searching needs to be performed, then that's a different question entirely. – Lightness Races in Orbit Mar 15 '11 at 00:22
  • Works perfect for me. Thanks bud ! – ItsJhonny Apr 13 '21 at 07:44
-1

there are great libraries for crawling websites like cURL but in your case it seems to be an overkill to use it. If you want to use the cURL library I recommend the Snoopy-class to you which is very simple to use.

Felix

Felix Geenen
  • 2,465
  • 1
  • 28
  • 37