我一直在試圖使用的preg_replace我的字符串裏替換期到逗號。替換。同在一個字符串
例如,
<?php
$string = "Hey you.";
$new_string = preg_replace("/./", ",", $new_string);
echo $new_string;
?>
我在這裏的錯誤在我知道,因爲我有圖案相當混亂。任何見解?謝謝。
我一直在試圖使用的preg_replace我的字符串裏替換期到逗號。替換。同在一個字符串
例如,
<?php
$string = "Hey you.";
$new_string = preg_replace("/./", ",", $new_string);
echo $new_string;
?>
我在這裏的錯誤在我知道,因爲我有圖案相當混亂。任何見解?謝謝。
使用str_replace
$new_string = str_replace(".", ",", $new_string);
與您正則表達式的問題是,你有沒有逃過.
,並.
是匹配任何字符。
你可以做到這一點
$new_string = preg_replace("/\./", ",", $new_string);
感謝您的評論和答覆。學到了新東西。 – 2011-12-17 16:56:12
然後你,@Dee應該接受這個答案:) – TimWolla 2011-12-17 17:05:04
我在等待接受倒計時。我會盡快接受這個計時器讓我:) – 2011-12-17 17:05:56
我讀一段時間以前,strtr
比str_replace
更快。這可能會或可能不會仍然是真實的:
$new_string = strtr($new_string, '.', ',');
嘗試:
<?php
$string = "Hey you.";
$new_string = preg_replace('/\./', ',', $new_string);
echo $new_string;
?>
有['preg_quote'(http://php.net/manual/en/function.preg-quote。 PHP)的一個原因;) – hakre 2012-08-27 07:20:03