我有一个像这样的字符串
<a href="http://www.example1.com"><b>12345</b> - John George<br><span>some_text1</span></a>
<a href="http://www.example2.com"><b>67890</b> - George Jerry<br><span>some_text2</span></a>
使用preg_match_all(PHP)我希望能够提取url,id和name,但我还没想出好的 sPattern (见下文):
$sPattern = "/<a href=\"(.*?)\"><b>(.*?)<\/b>\" - (.*?)\"<br>(.*?)/";
preg_match_all($sPattern, $content, $aMatch);
答案 0 :(得分:2)
我谦虚地建议使用像DOMDocument
这样的HTML解析器:
$html = '<a href="http://www.example1.com"><b>12345</b> - John George<br><span>some_text1</span></a>
<a href="http://www.example2.com"><b>67890</b> - George Jerry<br><span>some_text2</span></a>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
$data = array();
foreach($anchors as $anchor) {
$href = $anchor->nodeValue; // get the anchor href
$b = $anchor->firstChild->nodeValue; // get the b tag value
$data[] = array('href' => $href, 'id' => $b);
}
echo '<pre>';
print_r($data);
答案 1 :(得分:1)
如果你写一些更具体的模式可能会更好,试试这个:
$sPattern = "/<a href=\"([ˆ"]+)\"><b>(\d+)<\/b> - ((\w+ )*\w+)<br><span>([^<]+)<\/span><\/a>/";