PHP:拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5159086/
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 16:59:01  来源:igfitidea点击:

PHP: Split string

phpstringsplit

提问by Yoni Mayer

How do I split a string by .delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?

如何.在 PHP 中通过分隔符分割字符串?例如,如果我有 string "a.b",我怎么得到"a"

回答by NikiC

explodedoes the job:

explode工作:

$parts = explode('.', $string);

You can also directly fetch parts of the result into variables:

您还可以直接将部分结果提取到变量中:

list($part1, $part2) = explode('.', $string);

回答by Dan

explode('.', $string)

explode('.', $string)

If you know your string has a fixed number of components you could use something like

如果你知道你的字符串有固定数量的组件,你可以使用类似的东西

list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;

Prints:

印刷:

object
attribute

回答by Chris Baker

$string_val = 'a.b';

$parts = explode('.', $string_val);

print_r($parts);

Docs: http://us.php.net/manual/en/function.explode.php

文档:http: //us.php.net/manual/en/function.explode.php

回答by smottt

The following will return you the "a" letter:

以下将返回“a”字母:

$a = array_shift(explode('.', 'a.b'));

回答by jondavidjohn

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

Returns an array of split elements.

返回一个分割元素数组。

回答by Ujjwal Manandhar

to explode with '.' use

用 '.' 爆炸 用

explode('\.','a.b');