我应该使用哪个正则表达式?
如果这是我的字符串:“blablabla (blablabla)”。 我应该使用哪个正则表达式以返回的方式分割字符串:
- “blablabla”
- “(blablabla)”
顺便说一句,我想使用函数 mb_split。
编辑 该字符串也可以是“blablabla blablabla blablabla (blablabla)”。 因此,explode() 将无法工作。
编辑,这就是我现在使用的:
例如, $name = "blabla blabla blabla blabla (blabla)";
$name = explode(' ', $name);
$last = array_pop($name);
$sentence = null;
foreach ($name as $names) {
$sentence .= $names.' ';
}
$sentence = mb_substr($title, 0, -1, 'UTF-8');
If this is my string : "blablabla (blablabla)".
Which regular expression should i use to split the string in such a way that i get returned:
- "blablabla"
- "(blablabla)"
Btw, I want to use the function mb_split.
EDIT
The string can alsob be "blablabla blablabla blablabla (blablabla).
explode() wouldn't work because of this.
EDIT, this is what i use now:
for example, $name = "blabla blabla blabla blabla (blabla)";
$name = explode(' ', $name);
$last = array_pop($name);
$sentence = null;
foreach ($name as $names) {
$sentence .= $names.' ';
}
$sentence = mb_substr($title, 0, -1, 'UTF-8');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上,您不需要正则表达式来完成按空格分割两个单词这样简单的任务。使用
explode()
代替Actually, you do not need regex to do such easy task as splitting two words by space. Use
explode()
instead根本不需要正则表达式,只需使用
explode
:您所做的就是寻找一个空格!
No regular expression at all, just use
explode
:All you're doing is looking for a space!
如果您要匹配后跟“(”的空格,则分割的模式可以是:
If you're matching on a space that is followed by a '(', the pattern on which to split can be:
您不需要编写正则表达式。
对于 EXPLODE,
第一个参数将是您想要将字符串导出到数组中的 neddle..第二个参数必须是字符串..
you dont required to write Regular Expression.
for EXPLODE
first argument will be neddle by which you want to export string into array..and second argument must be string ..
我会使用爆炸,因为这里不需要正则表达式。如果您仍然需要 mb_split,则可以在任何假定为空白的地方(换行符、换行符、空格、制表符等)进行拆分:
mb_split("\s", "hello world")
。请参阅文档。 (我实际上将示例粘贴在这里:))
I would use explode, because no regex is needed here. If you still want mb_split, you can split at anything that is assumed a whitespace (newline, linebreak, space, tab etc):
mb_split("\s", "hello world")
.Look at docs. (I actually pasted the example here :))