3

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() ?

Shikiryu
  • 10,180
  • 8
  • 49
  • 75

6 Answers6

6

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

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
3

Use strpos and substr. Get the index of the first space with strpos and then use that index as the end of your substr. That will save you exploding each line.

UFL1138
  • 632
  • 3
  • 10
1

Use the String Token function http://www.w3schools.com/php/func_string_strtok.asp

0

Use a regex. This should get the match before first space. (?:^|(?:\.\s))(\w+)

dudemanbearpig
  • 1,264
  • 3
  • 12
  • 19
  • 2
    I don't believe that will be any faster than explode. Or will it? – UFL1138 Oct 01 '13 at 14:26
  • @UFL1138 Its faster because regular expressions, if used correctly, stop as soon as they have the first match and don't operate on the entire string in compare to explode. – dudemanbearpig Oct 01 '13 at 14:30
  • Still there's a lot of overhead to instantiate the regex engine. At any rate it will certainly not be faster than using strpos and substr. – UFL1138 Oct 01 '13 at 17:54
0

You should try this :

list($firstWord) = explode(" ",trim($line));

see here

Community
  • 1
  • 1
devict
  • 589
  • 4
  • 8
0

You can also use explode with the limit:

explode(" ", $line, 1);

Although this is much faster than explode without the limit, it is not as fast as the substr() solution suggested by the user: Set Sail Media.

In my tests with 100K lines, every line having around 10K words:

explode       : 175s
explode+limit : 0s
substr()      : 0s

With 1M lines, every line having around 20K words:

explode       : too long :-)
explode+limit : 12s
substr()      : 0s
Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53