如果 space 不包含在斜杠后面,如何在斜杠后面添加 space?
How do I add space behind slash if space not contain behind slash?
这是找到类似正则表达式格式的方法吗?
我可能有一个字符串或者可能是一个像
这样的行列表
Human have hands/fingers/feet and so on
如果space后面没有斜线,我想在斜线后面加上spaceHuman have hands/ fingers/ feet and so on
但是,如果 space 包含后面的字符串,我不想添加额外的 space。只想在字符串的其余部分添加 space,斜杠后面没有 space。
例如 hands/fingers/ feet
到 hands/ fingers/ feet
下面的代码肯定可以工作(离线测试):
<?php
$string = "hands/fingers/feet";
$spaceArr = explode("/",$string);
$modify_string = '';
foreach($spaceArr as $val){
$modify_string .= $val."/ ";
}
echo $modify_string;
// Output is hands/ fingers/ feet
?>
您可以在手/手指/脚中添加任意多space,但输出总是像手/手指/脚
是的,当然要用 RegEx 来解决这个问题
$count = null;
$returnValue = preg_replace('/\/([^ ])/', '/ ', 'hands/fingers/ feet', -1, $count);
说明
/\/([^ ])/
此正则表达式仅匹配 /
而没有 space 并替换为“/ $1”。这里$1是/
之后的字符
是的,消极展望是最好的方法。
$str = preg_replace('/\/(?!\s)/', '/ ', $str);
你可以在[这里][1]看到它
这使用了消极的展望。所以它基本上说:
只有当 /
后面没有 space 时才匹配 /
。此外,前瞻不消耗匹配中的任何字符,因此如果没有前瞻匹配,则无需替换字母。
特别感谢@ShalithaSuranga post我最初想到的相同答案,但在我有机会 post 它之前,这迫使我付出额外的努力来使用展望未来。
我通常不使用它们,因为它们比常规匹配模式更令人困惑。
也就是说,任何一种方法都可能比爆炸方法或其他更冗长的方法更快。但是,在这种情况下,速度可能不是问题,我会说选择对您来说最有意义并且更容易阅读和理解的那个。
可读性始终是第一位的,其次是功能,最后是性能。
如果你想要一个简短的非 regx 答案,你也可以使用这个
$str = "hands/fingers/feet";
echo implode("/ ", array_map('trim', explode("/", $str)));
所见here
对于这个,array_map
将函数回调作为第一个参数(可以是字符串)并将其应用或映射到数组中的每个项目。在这种情况下,我们希望 trim
从字符串的两边删除 whitespace 。然后我们将它重新组合在一起。这样一个像这样的数组就不会爆炸 one/ two
[
'one',
' two'
]
将修剪掉白色 space,然后我们用 '/ '
将其内爆并全部设置。
你可以用这个正则表达式来完成:
\/(\s)?
演示:https://regex101.com/r/c67Mh9/1/
PHP 例子:
echo preg_replace('@\/(\s)?@', '/ ', "Human have hands/fingers/ feet and so on");
使用 explode()
或尝试下面的 str_replace()
示例
请尝试str_replace()
:
<php
$words = 'hands/fingers/ feet'; /** Your example words **/
$words = str_replace('/', '/ ', $words); /** Replace any slash with a slash AND a space **/
$words = str_replace('/ ', '/ ', $words); /** Replace slash with 2 spaces with slash with 1 space as this is what you need **/
echo $words; /** Output: hands/ fingers/ feet **/
?>
我希望这会把你带到正确的方向..
这是找到类似正则表达式格式的方法吗?
我可能有一个字符串或者可能是一个像
这样的行列表Human have hands/fingers/feet and so on
如果space后面没有斜线,我想在斜线后面加上spaceHuman have hands/ fingers/ feet and so on
但是,如果 space 包含后面的字符串,我不想添加额外的 space。只想在字符串的其余部分添加 space,斜杠后面没有 space。
例如 hands/fingers/ feet
到 hands/ fingers/ feet
下面的代码肯定可以工作(离线测试):
<?php
$string = "hands/fingers/feet";
$spaceArr = explode("/",$string);
$modify_string = '';
foreach($spaceArr as $val){
$modify_string .= $val."/ ";
}
echo $modify_string;
// Output is hands/ fingers/ feet
?>
您可以在手/手指/脚中添加任意多space,但输出总是像手/手指/脚
是的,当然要用 RegEx 来解决这个问题
$count = null;
$returnValue = preg_replace('/\/([^ ])/', '/ ', 'hands/fingers/ feet', -1, $count);
说明
/\/([^ ])/
此正则表达式仅匹配 /
而没有 space 并替换为“/ $1”。这里$1是/
是的,消极展望是最好的方法。
$str = preg_replace('/\/(?!\s)/', '/ ', $str);
你可以在[这里][1]看到它
这使用了消极的展望。所以它基本上说:
只有当 /
后面没有 space 时才匹配 /
。此外,前瞻不消耗匹配中的任何字符,因此如果没有前瞻匹配,则无需替换字母。
特别感谢@ShalithaSuranga post我最初想到的相同答案,但在我有机会 post 它之前,这迫使我付出额外的努力来使用展望未来。
我通常不使用它们,因为它们比常规匹配模式更令人困惑。
也就是说,任何一种方法都可能比爆炸方法或其他更冗长的方法更快。但是,在这种情况下,速度可能不是问题,我会说选择对您来说最有意义并且更容易阅读和理解的那个。
可读性始终是第一位的,其次是功能,最后是性能。
如果你想要一个简短的非 regx 答案,你也可以使用这个
$str = "hands/fingers/feet";
echo implode("/ ", array_map('trim', explode("/", $str)));
所见here
对于这个,array_map
将函数回调作为第一个参数(可以是字符串)并将其应用或映射到数组中的每个项目。在这种情况下,我们希望 trim
从字符串的两边删除 whitespace 。然后我们将它重新组合在一起。这样一个像这样的数组就不会爆炸 one/ two
[
'one',
' two'
]
将修剪掉白色 space,然后我们用 '/ '
将其内爆并全部设置。
你可以用这个正则表达式来完成:
\/(\s)?
演示:https://regex101.com/r/c67Mh9/1/
PHP 例子:
echo preg_replace('@\/(\s)?@', '/ ', "Human have hands/fingers/ feet and so on");
使用 explode()
或尝试下面的 str_replace()
示例
请尝试str_replace()
:
<php
$words = 'hands/fingers/ feet'; /** Your example words **/
$words = str_replace('/', '/ ', $words); /** Replace any slash with a slash AND a space **/
$words = str_replace('/ ', '/ ', $words); /** Replace slash with 2 spaces with slash with 1 space as this is what you need **/
echo $words; /** Output: hands/ fingers/ feet **/
?>
我希望这会把你带到正确的方向..