$ptn = "/^Response.+?[:] /";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "";
echo preg_replace($ptn, $rpltxt, $str);
“Moore Auto”是一个变量名,所以我只需要冒号和空格后面的文本。在这种情况下,所需的最终结果将是字符串“感谢您的反馈”。非常感激!
$ptn = "/^Response.+?[:] /";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "";
echo preg_replace($ptn, $rpltxt, $str);
“Moore Auto”是一个变量名,所以我只需要冒号和空格后面的文本。在这种情况下,所需的最终结果将是字符串“感谢您的反馈”。非常感激!
简单substr()
,像这样:
$str = 'Response from Moore Auto: Thanks for your feedback';
echo substr($str, strpos($str,':')+2); //echoes "Thanks for your feedback"
如果有多个冒号,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
}
?>
尝试
<?php
$ptn = "/^(Response.+[:])(.*?)/";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "$2";
echo preg_replace($ptn, $rpltxt, $str);
?>