[英]convert “, ” INTO “-” with regular expressions using PHP
I want to obtain this 我想获得这个
dog-cat-mouse
狗猫老鼠
From each one of those 从其中的每一个
What i came up with is 2 preg_replace 我想出的是2 preg_replace
$str = preg_replace('/[,\s]/', '-', $str);
$str = preg_replace('/--/', '-', $str);
works on my local server BUT does not work on production, it gives me 在我的本地服务器上工作但无法在生产中工作,它给了我
dog, cat, mouse -> dog--cat--mouse
狗,猫,老鼠->狗-猫-老鼠
which is not what I want 这不是我想要的
You need +
quantifier for your [,\\s]
character set. 您的
[,\\s]
字符集需要+
量词。
What it changes is that it now means not "any comma or a whitespace character" but "any consecutive commas and whitespace characters" 它所更改的是,它现在不是“任何逗号或空格字符”,而是“任何连续的逗号和空格字符”
preg_replace('/[,\s]+/', '-', $str)
preg_replace('/,\h*|\h+/', '-', $str)
使用\\ h代替\\ s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.