Monday, 15 March 2010

string - Extracting substring ending with word not working in PHP -



string - Extracting substring ending with word not working in PHP -

i trying extract substring string in php. extracted substring should end word. tried next code , i'm not getting output.

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

i want substring end word. here, want output very cool , friendly instead of very cool , friend output when seek substr($userresult->aboutme, 0, 20);. doing wrong? can help me prepare this?

thanks in advance.

you're useing strpos(), looks start of string. want strrpos() (r=reverse):

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

you don't want utilize offset strpos(), because might work in situation, if first few words shorter/longer, no longer works.

php string codeigniter substr

No comments:

Post a Comment