在preg_replace()中遇到问题

时间:2011-12-29 09:27:06

标签: regex php-5.3

我要使用"hello world."替换"hi" from "this is nice place. <br> hello world.<br>thanks and regards"以及preg_replace()之后的任何字符

代码就是那样

$arr1 = array('/hello world./');
$arr2 = array('hi');
$str = "this is nice place. <br> hello world.<br>thanks and regards.";
echo preg_replace($arr1, $arr2, $str);

但有了这个,我不仅能够取代“你好世界”。来自$ str。但我希望正则表达式替换“hello world”之后的所有字符。也。

2 个答案:

答案 0 :(得分:1)


$arr1 = array('/hello world\..*$/');
$arr2 = array('hi');
$str = "this is nice place. <br> hello world.<br>thanks and regards.";
echo preg_replace($arr1, $arr2, $str);

答案 1 :(得分:0)

$arr1 = array('/hello world\..*$/');

第一个点由\转义,否则它是一个含有“任何字符”的运算符。以下是“任意字符”(.)任意次(*)后跟字符串结尾($)。

相关问题