可能重复:
php regex获取href标记内的字符串
我有一个文本文件,其中多次出现href标记。
我希望获得这些
href='…'的内容并将其打印到屏幕上。我怎样才能做到这一点?主要问题是编写正确的正则表达式。
开始:
$pageData = file_get_contents('your.txt');
if(preg_match_all('/<a's+href=["'']([^"'']+)["'']/i', $pageData, $links, PREG_PATTERN_ORDER))
$all_hrefs = array_unique($links[1]);
现在您在$all_href
中拥有了所有唯一的href;
如果你想显示它们:
foreach($all_href as $href)
{
echo $href;
}
preg_match_all('|<a href="(.+)">|', $file_content, $matches);
print_r($matches);
未经测试,但应能正常工作