PHP爆炸空间

时间:2013-11-12 12:13:19

标签: php split explode

字符串输入示例:

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

我怎样才能实现这一目标?感谢。

2 个答案:

答案 0 :(得分:4)

使用explode

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

答案 1 :(得分:1)

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

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

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