-4

字符串输入示例:

$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';

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

4

2 回答 2

4

使用爆炸limit选项

list($post_code, $name)  = explode(" ", $var_2, 2);
于 2013-11-12T12:15:24.620 回答
1

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

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

访问官方explode()手册页以获取更多示例。

于 2013-11-12T12:16:30.183 回答