user3388884 user3388884 - 4 years ago 150
PHP Question

How to use explode and get first element in one line in PHP?

$beforeDot = explode(".", $string)[0];

This is what I'm attempting to do, except that it returns syntax error. If there is a workaround for a one liner, please let me know. If this is not possible, please explain.

Answer Source

The function array dereferencing was implemented in PHP 5.4, so if you are using an older version you'll have to do it another way.

Here's a simple way to do it:

$beforeDot = array_shift(explode('.', $string));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download