[英]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個參數
“ , ” =>這是您要替換的
“” =>這是將替換第一個參數值的值。 我們傳遞空白,以便它將逗號替換為空白
這是您要替換的字符串。
正則表達式: (?<!\\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.