0

我有以下代码应该将提供的字符串与 $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] 中。但是,匹配似乎不起作用。有任何想法吗?

4

2 回答 2

1

不幸的是,您的建议没有奏效。

在浏览 html 代码几个小时后,我意识到正则表达式只是在冒号后面有一个空格。因此,代码片段现在看起来像这样:

$pattern = '#GENE: <b>(.*)</b>#i';
preg_match($pattern1, $contents, $match1);
if (isset($match1[1]))
{
    $found_flag = $match1[1];
}
于 2012-09-20T07:16:31.037 回答
0

尝试这个:

preg_match( '#GENE:<b>([^<]+)</b>si#', $contents, $match );
$found_match = ( isset($match[1]) ? $match[1] : false );
于 2012-08-10T23:24:36.720 回答