如何获得找到的值,而不是替换?使用 preg_replace php

how to get the value found, instead of replacing? using preg_replace php

如何获取找到的值,而不是替换?

echo $response=$htmlParserA->find("/tbody/tr/td",5)->plaintext."<br>";
$str=preg_replace("/[0-9][0-9][0-9]/","",$response);
echo $str;

回复:
在 HTTP/1.1 301 永久移动之前
HTTP/1.1 永久移动后

如何获得 301?

答案:

$str=preg_match("/[0-9][0-9][0-9]/",$response,$asd); 
echo $asd[0];

preg_match 会为所欲为:

$input = "HTTP/1.1 301 Moved Permanently";
$matches = array();
$found = preg_match("/\d{3}/", $input, $matches);
if($found)
{
    echo "Code = " . $matches[0];
}

如果需要,您可以使模式更严格:

$found = preg_match("/HTTP\/1\.1\s(\d{3})/", $input, $matches);
if($found)
{
    echo "Code = " . $matches[1];
}