我有像 +a +string +of +words +with +different +lengths
我想使用php的preg_replace来删除+,仅从1-3个字符长的单词中删除+,包括+。所需的输出是a +string of +words +with +different +lengths
如果不捕获群组,
(?<= |^)\+(?=\w{1,3}\b)
您最终的PHP代码是,
<?php
$string = '+a +string +of +words +with +different +lengths';
$pattern = "~(?<= |^)\+(?=\w{1,3}\b)~";
$replacement = "";
echo preg_replace($pattern, $replacement, $string);
?>
输出:
a +string of +words +with +different +lengths
解释:
(?<= |^)\+(?=\w{1,3}\b)
(?<= |^)
正向后方仅在起点或空间后方看。\+
文字+符号(?=\w{1,3}\b)
+
符号后面的字符必须是(1到3)个单词字符,然后再跟一个边界字符。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句