2011-12-17 109 views
-1

我一直在试图使用的preg_replace我的字符串里替换逗号替换。同在一个字符串

例如,

<?php 
$string = "Hey you."; 
$new_string = preg_replace("/./", ",", $new_string); 
echo $new_string; 
?> 

我在这里的错误在我知道,因为我有图案相当混乱。任何见解?谢谢。

+0

有['preg_quote'(http://php.net/manual/en/function.preg-quote。 PHP)的一个原因;) – hakre 2012-08-27 07:20:03

回答

6

使用str_replace

$new_string = str_replace(".", ",", $new_string); 

与您正则表达式的问题是,你有没有逃过.,并.是匹配任何字符。

你可以做到这一点

$new_string = preg_replace("/\./", ",", $new_string); 
+0

感谢您的评论和答复。学到了新东西。 – 2011-12-17 16:56:12

+0

然后你,@Dee应该接受这个答案:) – TimWolla 2011-12-17 17:05:04

+0

我在等待接受倒计时。我会尽快接受这个计时器让我:) – 2011-12-17 17:05:56

1

我读一段时间以前,strtrstr_replace更快。这可能会或可能不会仍然是真实的:

$new_string = strtr($new_string, '.', ','); 
0

尝试:

<?php 
$string = "Hey you."; 
$new_string = preg_replace('/\./', ',', $new_string); 
echo $new_string; 
?>