1
$ptn = "/^Response.+?[:] /";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "";
echo preg_replace($ptn, $rpltxt, $str);

“Moore Auto”是一个变量名,所以我只需要冒号和空格后面的文本。在这种情况下,所需的最终结果将是字符串“感谢您的反馈”。非常感激!

4

3 回答 3

3

简单substr(),像这样:

$str = 'Response from Moore Auto: Thanks for your feedback';
echo substr($str, strpos($str,':')+2);  //echoes "Thanks for your feedback"
于 2012-11-28T19:31:25.717 回答
0

如果有多个冒号,Damiens 解决方案不起作用。如果第一部分不包含冒号,这应该始终有效:

<?php 
$ptn = "/^Response[^:]+:\s*(.*)$/";
$str = "Response from Moore Auto: Thanks for your feedback";
if (preg_match($ptn, $str, $match)) {
    $text = $match[1];
    echo $text; //Thanks for your feedback
}
?>
于 2012-11-28T19:33:08.927 回答
0

尝试

<?php 
$ptn = "/^(Response.+[:])(.*?)/";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "$2";
echo preg_replace($ptn, $rpltxt, $str);
?>
于 2012-11-28T19:33:10.043 回答