删除多个逗号正则表达式 [英] Remove multiple commas regex
问题描述
有人可以让我知道如果在PHP中相邻的逗号会如何删除.
Can someone please let me know how I would remove multiple commas if they were next to one another in PHP.
$address= rtrim(preg_replace('!\s+!', ' ', $address_line_1.' '.$address_line_2.', '.$town_village_city.', '.$state_province_county.', '.$postal_code.', '.$country),',');
//return preg_replace('/([!?,.])+/',' ',$address);
return preg_replace('!\,+!',' ',$address);
例如,我可能有一个没有Town_village的地址,因此以多个逗号结尾.
For example I may have an address with no town_village so end up with multiple commas.
还有没有人有很好的资源来测试PHP正则表达式?
Also anyone good resources for testing PHP regex?
推荐答案
假定这些逗号之间没有空格,您可以简单地用一个逗号替换多个逗号:
assuming there are no spaces between these commas, you can simply replace multiple commas with one comma:
$cleadAddress = preg_replace('/,+/', ',', $address);
尽管您要用逗号(!)替换每个逗号,但是您也要替换多个逗号.是否值得尝试使用更复杂的正则表达式来节省一些执行时间,这取决于您的确切数据.
although you are replacing every comma with a comma (!), you also replace multiple commas. It depends on your exact data if a more complex regex is worth the effort to save some execution time.
这篇关于删除多个逗号正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!