duanhe0817825 2018-10-10 17:49 采纳率: 100%
浏览 58
已采纳

需要正则表达式在“点”之前和之后添加空格,但在十进制数字上没有

i need a regex that replaces the char "." with a blank space before and after " . ", but only if the point is not part of a number (decimal point):

example:

test. of my country.brazil, should be 38.45

It should return:

test . of my country . brazil, should be 38.45

Any one can help ?

Thanks

  • 写回答

1条回答 默认 最新

  • dongzilu0178 2018-10-10 17:53
    关注

    This should do the trick:

    <?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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?