开发者

How do I detect the existence of a particular page in a URL?

I am trying to 开发者_JS百科check whether a particular URL is present in a page supplied by another website.

I need to check whether or not the given URL contains a link to my website.

I am using PHP, and I would like to use preg.


If you know the url you are looking for, can't you use strpos()?

if (strpos($your_html_page, 'your_url') !== false) {
    echo 'The web web page has your_url in it.';
}

This is a simple match - but it doesn't tell you whether it's a hyperlink - just if the url is somwehere in the page. If you want to verify is actually a hyperlink than this approach or a simple regex is not going to help (at least a regex is likely to be fiddly and unreliable). You need to parse the DOM properly, extract out the <a> elements and check against the href attribute.


And for the parsing part you could use the simple html dom parser

html = file_get_html('http://stackoverflow.com/');

// Find all links 
foreach($html->find('a') as $element){
   if( preg_match($your_website_url, $element->href) > 0){
      //do something
   }
}


Well, you fetch the content using streams and then you parse it.


Recently i had to do that...

here is the regular expresion

preg_match_all( '/<a[^>]*href=[\'"]([^\'"]+)[\'"][^>]*>/i', $message, $links );

in th array $links you will have 2 arrays.

0 - The complete <a> tags

1 - The Url of the href attributes

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜