PHP implements the code to crawl all the hyperlinks in a Web page

Source: Internet
Author: User
Because you want to do something like a professional search engine recently, you need to crawl all the hyperlinks in your pages. Everyone help test all of a sudden, the following code can be for all standard hyperlinks.

Generic HTML standard hyperlink parameter gets the regular expression test

Because you want to do something like a professional search engine recently, you need to crawl all the hyperlinks in your pages.
Everyone help test all of a sudden, the following code can be for all standard hyperlinks.

The test code is as follows:

<?php
// --------------------------------------------------------------------------
File name:Noname1.php
Description: Universal link parameter get regular expression test
REQUIREMENT:PHP4 (http://www.php.net)
Copyright (C), Honestqiao, Reserved, all Rights.
Author:honestqiao (honestqiao@hotmail.com)
Parameter description:
$strSource: HTML pages that contain standard links
$strResult: Results of processing
Additional Instructions:
Standard links, using the links contained in <a></a> situation
// --------------------------------------------------------------------------
$strSource = <<<a href=1.htm>t1</a>
<a href= ' 2.htm ' >t2</a>
<a href= "3.htm" >t3</a>
<a href=4.htm class=link>t4</a>
HTML;
Preg_match_all ('/<a.*: \\t\\r\\n)? href=[\ ' "]? (. +?) [\'"]? (:(?: \\t\\r\\n) +.*? > (. +?) <\/a.*?>/sim ', $strSource, $strResult, Preg_pattern_order);
for ($i = 0; $i < count ($strResult [1]); $i + +)
{
printf ("%d href= (%s) title= (%s) \ n", $i, $strResult [1][$i], $strResult [2][$i]);
}

?>



If your test data conforms to the standard link but is not being processed here, please tell me the test data and your test environment.

Thank you.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.