Thursday, 16 October 2014

PHP: Get last part of string



I have dynamic list of names, for example: 

1) John Cena 
2) Nepresso USA Inc. 
3) Adam & Christie Rana


I need to fectch first and last name here, in this case I need result like: 
1) $array[0]=John , $array[1]=Cena
2) $array[0]=Nepresso , $array[1]=USA Inc. 
3) $array[0]=Adam , $array[1]=& Christie Rana

Solution:

The explode() function is an efficient way.

Just use its 3rd parameter which says how many of substrings you want to get.

Use$array = explode(' ',$string,2);


to get two substrings.

0 comments:

Post a Comment