639   PHP

一,正则表达式

$pattern="/<a.*?href?\s*=\s*[\'|\"]+?(.*?)[\'|\"]+?/i";
// <a匹配a标签
// <a.*?href?匹配href,a与href之间可能包含样式class或者空格
// <a.*?href?\s*=\s*匹配到=,=和href之间可能包含空格
// <a.*?href?\s*=\s*[\'|\"]+?匹配到'或者",href路径可能是单引号或者双引号标记
// <a.*?href?\s*=\s*[\'|\"]+?(.*?)匹配到url,并作为变量保存下来
// <a.*?href?\s*=\s*[\'|\"]+?(.*?)[\'|\"]+?匹配到'或者",href路径可能是单引号或者双引号标记
// 前后两个斜杠 / 表示正则表达式的范围,后面的 i 表示忽略大小写

 

二,测试代码

$subject = '<a href=\'test1\'>
<a class=\'test\' href=\'test2\'>
<a class=\'test\' href =\'test3\'>
<a class=\'test\' href= \'test4\'>
<a class=\'test\' href=\'test5\' javascript=\'void(0);\'>
<a href="test6">
<a class="test" href="test7">
<a class="test" href ="test8">
<a class="test" href= "test9">
<a class="test" href="test10" javascript="void(0);">';

$pattern="/<a.*?href?\s*=\s*[\'|\"]+?(.*?)[\'|\"]+?/";
preg_match_all($pattern,$subject,$matches);
var_dump($matches);

三,结果显示

 

C:\Users\luckybird\Desktop\test>php test.php
array(2) {
 [0]=>
 array(10) {
 [0]=>
 string(15) "<a href='test1'"
 [1]=>
 string(28) "<a class='test' href='test2'"
 [2]=>
 string(29) "<a class='test' href ='test3'"
 [3]=>
 string(29) "<a class='test' href= 'test4'"
 [4]=>
 string(28) "<a class='test' href='test5'"
 [5]=>
 string(15) "<a href="test6""
 [6]=>
 string(28) "<a class="test" href="test7""
 [7]=>
 string(29) "<a class="test" href ="test8""
 [8]=>
 string(29) "<a class="test" href= "test9""
 [9]=>
 string(29) "<a class="test" href="test10""
 }
 [1]=>
 array(10) {
 [0]=>
 string(5) "test1"
 [1]=>
 string(5) "test2"
 [2]=>
 string(5) "test3"
 [3]=>
 string(5) "test4"
 [4]=>
 string(5) "test5"
 [5]=>
 string(5) "test6"
 [6]=>
 string(5) "test7"
 [7]=>
 string(5) "test8"
 [8]=>
 string(5) "test9"
 [9]=>
 string(6) "test10"
 }
}



Leave a Reply

Your email address will not be published. Required fields are marked *