简体   繁体   English

Preg_match_all和html标签

[英]Preg_match_all and html tags

I am trying to get the content between <b>Author:</b> and <br> from the HTML below with preg_match_all function however it keeps returning empty arrays. 我正在尝试使用preg_match_all函数从下面的HTML获取<b>Author:</b><br>之间的内容,但是它会不断返回空数组。 I need the middle-line HTML output, please help me. 我需要中线HTML输出,请帮助我。

Here's the text: 这是文本:

<b>Author:</b> <a href="http://link.com" target="_blank" rel="nofollow">Name</a><br />

Here's the script I use: 这是我使用的脚本:

preg_match_all("'<b>Author:</b> ([^<]*)<br />'", $page, $preg_author);
$author = $preg_author[1]; 
print_r($preg_author);

your regexp can't work you're looking for ([^<]*) basically this will fail when it will encounter the <a tag you should try this one 您的正则表达式无法正常工作,您正在寻找([^ <] *),当遇到<a标签时,这将失败,您应该尝试使用此标签

preg_match_all("'<b>Author:</b> (.*(?=<br />))'", $page, $preg_author);

basically it will catch any character (no newline) until it will encounter a 基本上它将捕获任何字符(没有换行符),直到遇到
tag 标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM