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

如何获取找到的值,而不是替换?使用preg_替换php,php,web,Php,Web,如何获取找到的值,而不是替换 echo $response=$htmlParserA->find("/tbody/tr/td",5)->plaintext."<br>"; $str=preg_replace("/[0-9][0-9][0-9]/","",$response); echo $str; preg_match会做你想做的事: $input = "HTTP/1.1 301 Moved Permanently"; $matches = array(); $foun

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

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

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

preg_匹配而不是preg_replace@developerwjk不preg|u match return true | | false.preg|u grep然后:@developerwjk我都读过了!它需要一个数组。我有一根绳子。你知道php吗?我第一次就知道我是对的:preg_match。是的,它返回true/false,但您传入一个数组,该数组将填充匹配项。我修复了您的代码更紧凑的$str=preg_match(“/[0-9][0-9][0-9]/”,$response,$asd);echo$asd[0];
$found = preg_match("/HTTP\/1\.1\s(\d{3})/", $input, $matches);
if($found)
{
    echo "Code = " . $matches[1];
}