PHP: Split string [duplicate]

2018-12-31 13:58发布

问题:

This question already has an answer here:

  • How can I split a comma delimited string into an array in PHP? 6 answers

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

回答1:

explode does the job:

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

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

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


回答2:

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


回答3:

$string_val = \'a.b\';

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

print_r($parts);

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



回答4:

The following will return you the \"a\" letter:

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


回答5:

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

Returns an array of split elements.



回答6:

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

http://php.net/manual/ru/function.explode.php



回答7:

to explode with \'.\' use

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


标签: php string split