PHP Explode Function

I'm just wondering if there is a way to write this code in only one line.

$exp = explode(" ", $text);
$cut = $exp[0];

Thus, without the need to assign variables.

thank

+5
source share
4 answers

If you need only the first part, then avoid traversing the array with strtok:

$cut = strtok($text, " ");

It cuts something from the line to the first delimiter (space in your case).

+8
source
$cut = preg_replace('/ [\s\S]*$/', '', $text);

http://codepad.org/yujJRnYS

+4
source
$var = reset(explode(" ", $text));
+3
source
$cut = substr ( $text, 0, strpos ( $text, ' ' ) );

OR

$cut = substr ( trim ( $text ), 0, strpos ( trim ( $text ), ' ' ) );
+3
source

All Articles