preg_match不匹配

时间:2012-08-08 13:49:22

标签: php regex preg-match

我正在尝试从HTML

中的transaction(123456)获取ID

似乎没有任何回报。

 $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];
}

3 个答案:

答案 0 :(得分:5)

因为你有字符串锚^的开头,而transaction(不在字符串的开头。删除(或添加一个非贪婪的匹配),它将工作(如this demo所示):

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

答案 1 :(得分:1)

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

应该是

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

答案 2 :(得分: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];
}

这是您想要的代码。 原因是,你的模式以“^”开头,这个字符的意思是,你希望“交易”出现在字符串“$ response”的前面,但是有“......”