php 分成两个变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:10:46  来源:igfitidea点击:

Split into two variables?

phpstringparsingexplode

提问by Latox

Say I have the following: "44-xkIolspO"

说我有以下几点: "44-xkIolspO"

I want to return 2 variables:

我想返回 2 个变量:

$one = "44";
$two = "xkIolspO";

What would be the best way to do this?

什么是最好的方法来做到这一点?

回答by Chandu

Try this:

尝试这个:

list($one, $two) = split("-", "44-xkIolspO", 2);

list($one, $two) = split("-", "44-xkIolspO", 2);

list($one, $two) = explode("-", "44-xkIolspO", 2);

回答by Vincent Ramdhanie

PHP has a function called preg_split()splits a string using a regular expression. This should do what you want.

PHP 有一个名为preg_split()的函数使用正则表达式拆分字符串。这应该做你想做的。

Or explode()might be easier.

或者explode()可能更容易。

    $str = "44-xkIolspO";
    $parts = explode("-", $str);
    $one = $parts[0];
    $two = $parts[1];