我有以下一段代码,它应该将提供的字符串与$contents匹配。 $contents变量具有通过file_get_contents()函数存储的网页内容:
if (preg_match('~<p style="margin-top: 40px; " class="head">GENE:<b>(.*?)</b>~iU', $contents, $match)){
$found_match = $match[1];
}
所述网页上的原始字符串如下所示:
<p style="margin-top: 40px; " class="head">GENE:<b>TSPAN6</b>
我想匹配并存储通过(.*?)在网页上找到的字符串"TSPAN6"到$match[1]。但是,匹配似乎不起作用。有什么想法吗?
不幸的是,您的建议不起作用。
经过几个小时的浏览 html 代码,我意识到正则表达式只是在冒号后面有一个空格。因此,代码片段现在如下所示:
$pattern = '#GENE: <b>(.*)</b>#i';
preg_match($pattern1, $contents, $match1);
if (isset($match1[1]))
{
$found_flag = $match1[1];
}
试试这个:
preg_match( '#GENE:<b>([^<]+)</b>si#', $contents, $match );
$found_match = ( isset($match[1]) ? $match[1] : false );