[英]How can I replace dot with comma but only for numbers in a string ? (PHP)
I want to replace all dots in a string to commas, but only for numbers (in PHP):我想将字符串中的所有点替换为逗号,但仅限于数字(在 PHP 中):
Like :喜欢 :
this is. an example. 15.68€
and it should be converted to something like this is. an example. 15,68€
它应该转换为这样的东西
this is. an example. 15,68€
this is. an example. 15,68€
Is there an easy preg_replace
or any other option ?是否有简单的
preg_replace
或任何其他选项?
Try this one试试这个
$str = "this is. an example. 15.68€";
function cleaner($matches)
{
return str_replace("." , "," , $matches["nrs"]);
}
$str = preg_replace_callback ("/(?P<nrs>[0-9]+.[0-9]+)/" , "cleaner" , $str);
echo $str;
I have used preg_replace_callback
.我用过
preg_replace_callback
。 The behavior of this function is almost identical to preg_replace()
, except for the fact that instead of replacement parameter, one should specify a callback.此函数的行为几乎与
preg_replace()
相同,除了应该指定回调而不是替换参数这一事实。 Check for more about it here . 在此处查看更多信息。 You can also test the live preview of my solution.
您还可以测试我的解决方案的实时预览。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.