How is it possible to explode a string using '/', if slash doesn't contain spaces before or after.
如果斜杠之前或之后不包含空格,如何使用'/'分解字符串。
I wish the string like "Flat Visors / Fitted Caps" could be treated as a string and "Flat Visors/Fitted Caps" could be explode using '/'.
我希望像“Flat Visors / Fitted Caps”这样的字符串可以被视为一个字符串,而“Flat Visors / Fitted Caps”可以使用'/'进行爆炸。
1 个解决方案
#1
2
You can use preg_split
with negative look-behind and negative look-ahead:
您可以将preg_split与负面后瞻和负前瞻一起使用:
preg_split('/(?<!\s)\\/(?!\s)/', $str);
If you wish the opposite, i.e. splitting even when there is space around and remove the space, again with preg_split
and optional space:
如果您希望相反,即使在周围有空间时拆分并移除空间,再次使用preg_split和可选空间:
preg_split('/\s*\\/\s*/', $str);
#1
2
You can use preg_split
with negative look-behind and negative look-ahead:
您可以将preg_split与负面后瞻和负前瞻一起使用:
preg_split('/(?<!\s)\\/(?!\s)/', $str);
If you wish the opposite, i.e. splitting even when there is space around and remove the space, again with preg_split
and optional space:
如果您希望相反,即使在周围有空间时拆分并移除空间,再次使用preg_split和可选空间:
preg_split('/\s*\\/\s*/', $str);