开发者

PHP Check if page contains

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开发者_JAVA技巧 find the code I provided.

Thanks!


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.


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.


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';
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜