2012-08-08 63 views
1

我想從transaction(123456)的ID在HTML的preg_match不匹配

它似乎並不返回任何東西。

$response = "</body> </html><script type='text/javascript'>transaction(123456);</script>"; 

preg_match('/^transaction\((\d+)\)/', $response, $match); 

print_r($match); 

if (is_numeric($match[1])) { 
    echo "Your ID: " . $match[1]; 
} 

回答

5

因爲你有串錨^的開始,transaction(是不是在字符串的開頭。刪除(或添加非貪婪匹配),它會工作(如圖this demo):

preg_match('/transaction\((\d+)\)/', $response, $match); 
1
preg_match('/^transaction\((\d+)\)/', $response, $match); 

應該

preg_match('/transaction\((\d+)\)/', $response, $match); 
1
$response = "</body> </html><script type='text/javascript'>transaction(123456);</script>"; 

preg_match('/transaction\((\d+)\)/', $response, $match); 

print_r($match); 

if (is_numeric($match[1])) { 
    echo "Your ID: " . $match[1]; 
} 

這是你的代碼想。 原因是,你的模式以「^」開頭,這個字符意味着你期望taht「transaction」出現在你的字符串「$ response」的前面,但是有「...」