How do I split a string by .
delimiter in PHP? For example, if I have the string "a.b"
, how do I get "a"
?
Asked
Active
Viewed 1.6e+01k times
79

Michał Perłakowski
- 88,409
- 26
- 156
- 177

Yoni Mayer
- 1,212
- 1
- 14
- 27
7 Answers
28
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

Peter Mortensen
- 30,738
- 21
- 105
- 131

Dan
- 3,490
- 2
- 22
- 27
9
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode

Peter Mortensen
- 30,738
- 21
- 105
- 131

Chris Baker
- 49,926
- 12
- 96
- 115
7
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));

smottt
- 3,272
- 11
- 37
- 44
-1
To explode with '.', use:
explode('\\.', 'a.b');

Peter Mortensen
- 30,738
- 21
- 105
- 131

Ujjwal Manandhar
- 2,194
- 16
- 20