0

I am trying to extract a substring from a string in PHP. The extracted substring should end with a word. I tried with the following code and I'm not getting any output.

if (strlen($userresult->aboutme) > 20)    // value of $userresult->aboutme is 'Very cool and friendly'
{   
   $description=substr($userresult->aboutme, 0, strpos($userresult->aboutme, ' ', 20));
} 
else 
{
    $description=$userresult->aboutme;
}
echo $description;     // not outputting any result

I want the substring to be end up with a word. Here, I want output as Very cool and friendly instead of Very cool and friend which is the output when we try with substr($userresult->aboutme, 0, 20);. What I am doing wrong? Can anyone help me to fix this?

Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77

3 Answers3

3

You're useing strpos(), which looks from the start of a string. You want strRpos() (r=reverse):

$description=substr($userresult->aboutme, 0, strrpos($userresult->aboutme, ' '));

You don't want to use the offset for the strpos(), because it might work in this situation, but if the first few words are shorter/longer, it no longer works.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • `Very cool and friendly dude!` what about this string as long? Output `Very cool and friendly` – Bora Jun 18 '14 at 07:37
0
// split in words
$words = explode(' ', $userresult->aboutme);
// remove the last word
array_pop($words);
// combine again
echo implode(' ', $words);

This is not respecting any other seperator as comma or dot. But you are not doing that either.

colburton
  • 4,685
  • 2
  • 26
  • 39
  • Not going to downvote because it's another perspective at a problem, but you should not do this. **NEVER** use array functions for simple string functions. String functions are (almost) always faster and less resource intensive – Martijn Jun 18 '14 at 07:33
  • And why is that, exactly? – Kemal Fadillah Jun 18 '14 at 07:34
  • Yeah @Martijn you got my curious, here. Is that this much of a performance issue? – colburton Jun 18 '14 at 07:35
  • Arrays are slow, string functions are very fast. You do the math :p – Martijn Jun 18 '14 at 07:35
  • I did a small benchmark: Your code 100.000x takes 1.5sec, my code 100.000x takes 0.9sec. The string function is 40% faster. – Martijn Jun 18 '14 at 07:49
  • Nice info, thx @Martijn! But it still is a valid answer unless the page is actually called more than 10.000 times – colburton Jun 18 '14 at 07:51
  • IMO it a working answer, not a valid answer, but we could argue about tthat all day :p In this stand-alone situation this doesnt matter, but but if you apply this tactic to your whole site, you're gonna notice 40% ;) – Martijn Jun 18 '14 at 08:07
0

Use like this

 if (strlen($userresult->aboutme) > 15)    // value of $userresult->aboutme is 'Very cool and friendly'
  {   
   $description=substr($userresult->aboutme, 0, strrpos($userresult->aboutme, ' '));
  } 
  else 
  {
   $description=$userresult->aboutme;
   }

  echo $description;     
Rajendra Yadav
  • 645
  • 3
  • 12