PHP 分解空格


PHP explode spaces

字符串输入示例:

$var = "9999-111 Google";
$var_2 = "9999-222 StackOverflow Web";
I want to get only the postcode and then the address.
$postcode_A = explode(" ", $var);
// postcode[0] returns '9999-111' - postcode[1] returns 'Google'
$postcode_B = explode(" ", $var_2);
// postcode[0] returns '9999-222' - postcode[1] returns 'StackOverflow'
// and I want postcode[1] to return 'StackOverflow Web';

我怎样才能做到这一点?谢谢。

使用分解的limit选项

list($post_code, $name)  = explode(" ", $var_2, 2);

explode()接受第三个参数:int $limit

list( $postcode, $address ) = explode( ' ', $var, 2 ); // limits number of breaks up to 2

访问官方 explode() 手册页了解更多示例。