Jump to content

[SOLVED] Field - Subtract First Word


EternalSorrow

Recommended Posts

I've been searching for several hours for an answer to this simple problem, but no luck, so I've come groveling here to seek guidance.

 

All I want to know is when a $field is retrieved from the database, how can I subtract, skip, trim or essentially eliminate the first word from a string?  Here's an example of what I want with the string of the $field and the output I'm going searching for:

 

$string = "The Scarlet Pimpernel";

 

$output = "Scarlet Pimpernel";

 

Be aware the first word could be anything, not necessarily the word 'the.'

Link to comment
https://forums.phpfreaks.com/topic/180693-solved-field-subtract-first-word/
Share on other sites

That won't work, you forgot the delimiter :P '~^[^\s]*\s~'

 

I was curious so I tested:

 

$words = str_word_count($string, 1);
array_shift($words);
implode(' ', $words);

 

vs

$string = preg_replace('~^[^\s]*\s~','',$string);

 

regex takes about ~1.6 times longer. Not that it matters at this scale.

I'm kinda surprised the preg_match wasn't faster, since str_word_count has to evaluate the whole string. I wonder if

 

$string = explode(' ',$string);
array_shift($string)
$string = implode(' ',$string);

 

might be faster than str_word_count

 

ltrim(strstr(' ', $foo));

substr(strstr(' ', $foo), 1);

substr($foo, strpos($foo, ' ')+1);

for ($i = 0; !isset($foo[$i]) || $foo[$i] != ' '; ++$i) {}
echo substr($foo, $i+1);

for ($i = 0, $l = strlen($foo); $i < $l && $foo[$i] != ' '; ++$i) {}
substr($foo, $i+1);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.