我收到了这段代码:
..some code here..
<a href="members/username1.12345/" class="username bigs" title="">Username 1</a>
..some code here..
..some code here..
<a href="members/username2.55334/" class="username bigs" title="">Username 2</a>
..some code here..
..some code here..
<a href="members/username3.23455/" class="username bigs" title="">Username 3</a>
..some code here..
..some code here..
<a href="members/username4.45545/" class="username bigs" title="">Username 4</a>
..some code here..
如何使用preg_match_all()获取用户名1,用户名2,用户名3,用户名4等。
我不想使用SimpleHTMLdom或其他..我想用preg_match_all()来做..怎么做?我试过很多正则表达式......但是没有一个正常工作..
答案 0 :(得分:1)
Regexp是个糟糕的主意。但仍然 - 试试这个:@<a href="members/.+/" class="username bigs" title="">(.+)</a>@U
$subject = '..some code here..
<a href=\"members/username1.12345/\" class=\"username bigs\" title=\"\">Username 1</a>
..some code here..
..some code here..
<a href=\"members/username2.55334/\" class=\"username bigs\" title=\"\">Username 2</a>
..some code here..
..some code here..
<a href=\"members/username3.23455/\" class=\"username bigs\" title=\"\">Username 3</a>
..some code here..
..some code here..
<a href=\"members/username4.45545/\" class=\"username bigs\" title=\"\">Username 4</a>
..some code here..';
$pattern = '@<a href="members/.+/" class="username bigs" title="">(.+)</a>@U';
$result = preg_match_all($pattern, $subject, $subpattern);
echo '<pre>'.print_r($subpattern, TRUE).'</pre>';