I need to get every first word of the lines in my $lines
. So I'm doing an foreach
on every line like this:
foreach ($lines as $n => $line) {
}
But then the next part, I need to grab only the first word. So I did this with exploding on a space like this:
$explode = explode(" ", $line);
echo $explode[0];
But this is very slow when I do it with many lines, is there a better and faster solution then using an explode()
?
Yes!
$var = substr( $line, 0, strpos( $line, ' ' ) );
substr()
trims a string using start position (0, the beginning) and length: http://php.net/manual/en/function.substr.php
We determine the length by using strpos()
to find the first occurrence of the search phrase (in this case, a space): http://php.net/manual/en/function.strpos.php
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments