开发者

How can I split a string at the first occurrence of "-" (minus sign) into two $vars with PHP?

开发者 https://www.devze.com 2023-01-11 20:14 出处:网络
How can I split a string at the first 开发者_如何学Pythonoccurrence of - (minus sign) into two $vars with PHP?

How can I split a string at the first 开发者_如何学Pythonoccurrence of - (minus sign) into two $vars with PHP?

I have found how to split on every "-" but, not only on the first occurrence.

example:

this - is - line - of whatever - is - relevant
$var1 = this
$var2 = is - line - of whatever - is - relevant

Note, also stripped the first "-" .


It's very simple, using an extra paramater to explode that many people don't realize is there:

list($before, $after) = explode('-', $source, 2);


$array = explode('-', 'some-string', 2);

Then you could do $var1=$array[0] and $var2=$array[1].


Here is what you need: using list() with explode():

list($var1, $var2) = explode(' - ', 'this - is - line - of whatever - is - relevant', 2);

Note the spaces around the "-" (minus sign)


You can use strtok function:

$first = strtok($string, '-');
0

精彩评论

暂无评论...
验证码 换一张
取 消