簡體   English   中英

從PHP中的字符串中去除逗號

[英]Strip commas from a string in PHP

我有以下字符串:

輸入:

$str = "I want to remove only comma from this string, how ?";

我想從$str刪除逗號,這是我的編程新手,我不了解正則表達式的工作原理。

使用str_replace

$str = "I want to remove only comma from this string, how ?";
$str = str_replace(",", "", $str);  

如您所見,我們在str_replace中傳遞了3個參數

  1. ” =>這是您要替換的

  2. “” =>這是將替換第一個參數值的值。 我們傳遞空白,以便它將逗號替換為空白

  3. 這是您要替換的字符串。

正則表達式: (?<!\\d)\\,(?!\\d)

(\\,|\\.)對於精確匹配的任一,.

(?!\\d)前面不應包含數字。

(?<!\\d)后面不應包含數字。

PHP代碼:

<?php

$str = "I want to remove only comma from this string, how. ? Here comma and dot 55,44,100.6 shouldn't be removed";
echo preg_replace("/(?<!\d)(\,|\.)(?!\d)/", "", $str);

輸出:

I want to remove only comma from this string how ? Here comma 55,44,100 shouldn't be removed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM