Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
基本上我有这个字符串:
"We spent 10minutes talking while traveling 20kms"
如何将其转换为:
"We spent 10 minutes talking while traveling 20 kms"
基本上将附加到单词的每个数字或数字分开。
你可以使用每个人的朋友,正则表达式!
$str = preg_replace("/\d+\B/", "$0 ", $str);
键盘。
这将匹配一系列紧随其后具有非单词边界的十进制数字。
或者,你可以这样做......
$str = preg_replace("/(\d)(\D)/", "$1 $2", $str);
这将匹配一个十进制数字,该数字后面紧跟一个非十进制数字。
这应该有效:
$str = preg_replace('/(\d)([a-z])/i', '$1 $2', $str);
\d匹配一个数字,[a-z]匹配一个字母。
\d
[a-z]