如何使用 preg_replace 删除字符串中的空格
How to remove spaces in a string using preg_replace
我要删除首字母之间的 space 并保留首字母和任何单词之间的 space。
我想得到这个结果。
A.J.P。简 B.V.
相反,我得到了这个结果
A.J。 P. Jane B. V.
$string = "A.J. P. Jane B. V.";
$string = preg_replace('/\.\s\[A-z]{1}\./', '.', $string);
echo $string;
试试这个,
$string = preg_replace('/\s+/', '', $string);
使用此规则 \.\s([A-Z]{1})\.
或 \.\s([A-Z])\.
没有明确限制
匹配 [dot][space][letter][dot]
和
替换为 ..
、[dot][letter][dot]
$string = preg_replace('#\.\s([A-Z]{1})\.#', '..', $string);
echo $string;
会输出
A.J.P. Jane B.V.
我要删除首字母之间的 space 并保留首字母和任何单词之间的 space。
我想得到这个结果。 A.J.P。简 B.V.
相反,我得到了这个结果 A.J。 P. Jane B. V.
$string = "A.J. P. Jane B. V.";
$string = preg_replace('/\.\s\[A-z]{1}\./', '.', $string);
echo $string;
试试这个,
$string = preg_replace('/\s+/', '', $string);
使用此规则 \.\s([A-Z]{1})\.
或 \.\s([A-Z])\.
没有明确限制
匹配 [dot][space][letter][dot]
和
替换为 ..
、[dot][letter][dot]
$string = preg_replace('#\.\s([A-Z]{1})\.#', '..', $string);
echo $string;
会输出
A.J.P. Jane B.V.