Test the regular expression acquisition for common HTML standard hyperlink Parameters
Because I recently want to create something similar to a professional search engine, I need to capture all the hyperlinks on the web page.
Let's test whether the following code can target all the standard hyperlinks.
The test code is as follows:
Copy codeThe Code is as follows:
<? Php
//--------------------------------------------------------------------------
// File name: Noname1.php
// Description: test the regular expression for obtaining common link parameters.
// Requirement: PHP4 (http://www.php.net)
// Copyright (C), HonestQiao, 2005, All Rights Reserved.
// Author: HonestQiao (honestqiao@hotmail.com)
// Parameter description:
// $ StrSource: HTML webpage with standard link
// $ StrResult: processing result
// Additional description:
// Standard link, use the link contained in the <a> </a> situation
//--------------------------------------------------------------------------
$ StrSource = <HTML
<A href00001.htm> t1 </a>
<A href0000'2.htm'> t2 </a>
<A href = "3.htm"> t3 </a>
<A href00004.htm class = link> t4 </a>
HTML;
Preg_match_all ('/<.*? (? : \ 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 processed here, please let me know the test data and your test environment.
Thank you.