php - 需要一个正则表达式在“点”之前和之后添加一个空格,但十进制数字没有
问题描述
我需要一个替换字符“。”的正则表达式。在“ . ”之前和之后有一个空格,但前提是该点不是数字的一部分(小数点):
例子:
test. of my country.brazil, should be 38.45
它应该返回:
test . of my country . brazil, should be 38.45
任何人都可以帮忙吗?
谢谢
解决方案
这应该可以解决问题:
<?php
$regex = '/([^0-9])\s?\.\s?([^0-9])/';
$string = 'test. of my country.brazil, should be 38.45';
$replace = '$1 . $2';
echo preg_replace($regex, $replace, $string);
//test . of my country . brazil, should be 38.45