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);
コードパッド。
これは、直後に単語以外の境界がある一連の 10 進数に一致します。
または、これを行うこともできます...
$str = preg_replace("/(\d)(\D)/", "$1 $2", $str);
これは、10 進数以外の数が直後に続く 10 進数と一致します。
これはうまくいくはずです:
$str = preg_replace('/(\d)([a-z])/i', '$1 $2', $str);
\d数字に[a-z]一致し、文字に一致します。
\d
[a-z]