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.