Zub*_*ir1 1 php regex preg-replace
我一直遇到麻烦,我想知道是否有人知道preg_replace正则表达式可以删除除了从字符串遇到的第一个空格以外的所有空格.
|实例|
我有以下字符串:"我的第一个姓氏"
我想要实现的是:"我的FirstLastName"
对不起,但我对正则表达式很糟糕:(所以任何帮助表示赞赏.
你实际上并不需要正则表达式,只需将字符串拆分为空格然后再将其连接起来就更快了.
$name = "My First Last Name"
$pieces = explode(" ", $name, 2); // split into 2 strings
// $pieces[0] is before the first space, and $pieces[1] is after it
// so we can make the new string joining them together
// and just removing all spaces from $pieces[1]
$newName = $pieces[0] . " " . str_replace(" ", "", $pieces[1]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
984 次 |
| 最近记录: |