2012-08-10 36 views
0

我有以下一段代碼,它應該將提供的字符串與$ contents匹配。 $內容變量通過的file_get_contents存儲網頁內容()函數:preg_match_all刮在html標籤之間找到的詞

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]。但是,匹配似乎不起作用。有任何想法嗎?

+0

瞭解哪些信息是靜態的以及哪些信息是字符串中的動態信息將很有用。你的模式適用於我的xampp btw。 '$ match [1] =='TSPAN6'' – 2012-08-10 23:30:08

回答

1

不幸的是,你的建議沒有工作。

經過幾個小時的瀏覽html代碼後,我意識到正則表達式只是冒號後面有一個空格。因此,代碼片段現在看起來像這樣:

$pattern = '#GENE: <b>(.*)</b>#i'; 
preg_match($pattern1, $contents, $match1); 
if (isset($match1[1])) 
{ 
    $found_flag = $match1[1]; 
} 
0

試試這個:

preg_match('#GENE:<b>([^<]+)</b>si#', $contents, $match); 
$found_match = (isset($match[1]) ? $match[1] : false);