'PHP: Split string [duplicate]

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



Solution 1:[1]

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

Solution 2:[2]

$string_val = 'a.b';

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

print_r($parts);

Documentation: explode

Solution 3:[3]

The following will return you the "a" letter:

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

Solution 4:[4]

Use:

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

explode

Solution 5:[5]

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

Returns an array of split elements.

Solution 6:[6]

To explode with '.', use:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 Peter Mortensen
Solution 3
Solution 4 Peter Mortensen
Solution 5 jondavidjohn
Solution 6 Peter Mortensen