【问题标题】:PHP - Remove DOTs from text but exclude numbers [duplicate]PHP - 从文本中删除点但排除数字[重复]
【发布时间】:2016-12-24 20:55:40
【问题描述】:

我想删除包含 numberstext 的字符串中的点 (.)。

例如:S.D.M.SSDMS

但我想保留 numbers 中的 (.) 原样。

例如:123.50123.50

我尝试了str_replace(),但它删除了所有 (.)s

如何在PHP 中做到这一点?

【问题讨论】:

    标签: php regex


    【解决方案1】:

    使用preg_replace

    $result = preg_replace('/(?<!\d)\.(?!\d)/', '', $string);
    

    在哪里

    • (?&lt;!\d) 是否定的后视,假设点前没有数字。
    • (?!\d) 是负前瞻,假设点后没有数字。

    【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2015-08-30
    相关资源
    最近更新 更多