在字符串的第一个空格处分开

时间:2022-08-22 13:16:38

I have a string like this:

我有一个像这样的字符串:

red yellow blue

红黄蓝

and I want to get an array like this :

我想得到一个像这样的数组:

Array ( [0] => red [1] => yellow blue )

数组([0] =>红色[1] =>黄色蓝色)

how to split at the first space in a string ? my code doesn't work

如何在字符串的第一个空格中拆分?我的代码不起作用

<?php
$str = "red yellow blue";
$preg = preg_split("/^\s+/", $str);
print_r($preg);
?>

please help me.

请帮帮我。

5 个解决方案

#1


54  

Use explode with a limit:

使用具有限制的爆炸:

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

Just a side note: the 3rd argument of preg_split is the same as the one for explode, so you could write your code like this as well:

只是旁注:preg_split的第三个参数与explode的相同,所以你也可以像这样编写你的代码:

$array = preg_split('#\s+#', $string, 2);

References:

参考文献:

PHP: explode

PHP:爆炸

PHP: preg_split

PHP:preg_split

#2


4  

<?php
$string = "red yellow blue";
$result = explode(" ", $string, 2);
print_r($result);
?>

just explode it

只是爆炸它

#3


3  

You can use explode function like this

你可以像这样使用爆炸功能

print_r(explode(' ', $str, 2));

It will set a limit. Check more about it here

它将设定一个限制。在这里查看更多相关信息

#4


2  

You can use explode, but if you aren't 100% sure you'll have the same # of spaces (explosions) every time, you can use ltrim to remove the first word and space

你可以使用explode,但如果你不是100%确定你每次都会有相同的空间(爆炸),你可以使用ltrim删除第一个单词和空格

<?php
$full='John Doe Jr.';
$full1=explode(' ', $full);
$first=$full1[0];
$rest=ltrim($full, $first.' ');
echo "$first + $rest";
?>

#5


-3  

You can use explode this way:

你可以用这种方式爆炸:

  $stringText  = "red yellow blue";
  $colours = explode(" ", $stringText);
  echo $colours[0]; //red 
  echo $colours[1]; //yellow
  echo $colours[2]; //blue

You can also get all the elements of $colours by foreach Loop, but in this case explode is better

您还可以通过foreach Loop获取$ colors的所有元素,但在这种情况下,爆炸更好

#1


54  

Use explode with a limit:

使用具有限制的爆炸:

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

Just a side note: the 3rd argument of preg_split is the same as the one for explode, so you could write your code like this as well:

只是旁注:preg_split的第三个参数与explode的相同,所以你也可以像这样编写你的代码:

$array = preg_split('#\s+#', $string, 2);

References:

参考文献:

PHP: explode

PHP:爆炸

PHP: preg_split

PHP:preg_split

#2


4  

<?php
$string = "red yellow blue";
$result = explode(" ", $string, 2);
print_r($result);
?>

just explode it

只是爆炸它

#3


3  

You can use explode function like this

你可以像这样使用爆炸功能

print_r(explode(' ', $str, 2));

It will set a limit. Check more about it here

它将设定一个限制。在这里查看更多相关信息

#4


2  

You can use explode, but if you aren't 100% sure you'll have the same # of spaces (explosions) every time, you can use ltrim to remove the first word and space

你可以使用explode,但如果你不是100%确定你每次都会有相同的空间(爆炸),你可以使用ltrim删除第一个单词和空格

<?php
$full='John Doe Jr.';
$full1=explode(' ', $full);
$first=$full1[0];
$rest=ltrim($full, $first.' ');
echo "$first + $rest";
?>

#5


-3  

You can use explode this way:

你可以用这种方式爆炸:

  $stringText  = "red yellow blue";
  $colours = explode(" ", $stringText);
  echo $colours[0]; //red 
  echo $colours[1]; //yellow
  echo $colours[2]; //blue

You can also get all the elements of $colours by foreach Loop, but in this case explode is better

您还可以通过foreach Loop获取$ colors的所有元素,但在这种情况下,爆炸更好