In PHP, what is the simplest way to return the portion of a string before the first occurrence of a specific character?
For example, if I have a string...
\"
To sum up there're 4 ways
strstr($str,' ',true);
strtok($str,' ');
explode(' ', $str)[0]; //slowest
substr($str, 0, strpos($str, ' '));
The difference is that if no delimiter found:
strstr returns false
strtok explode returns whole string
substr returns empty string
if unexpected troubles with multibyte appear then this example
$str = mb_strstr($str, 'и', true) ?: $str;