which regular expression should i use?
If this is my str开发者_如何学运维ing : "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');
Actually, you do not need regex to do such easy task as splitting two words by space. Use explode()
instead
$string = "blablabla (blablaba)";
explode(" ", $string);
No regular expression at all, just use explode
:
$mystring = "blablabla (blablaba)";
$mywords = explode(" ", $mystring);
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:
\s(?=\()
explode(" ", $string);
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 ..
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 :))
精彩评论