Sunday 15 March 2015

easyphp - php code to extract firstname and lastname from fullname -


I am starting a lot in php I want to remove the first name and last name from the full name. The following was my code: -

  & lt ;? Php $ str = "fname lname"; $ Lane = strlen ($ str); $ S = ""; ($ I = 0; i & lt; = $ len; $ i ++) for {$ sub = substr ($ str, $ i, ($ i + 1)); If ($ sub! = "") {$ S = s + $ sub; } Else {break;}} echo $ s; ? & Gt;   

The problem with this code is that the loop runs infinitely and a serious error occurs. Any help would be appreciated.

This is a quick and easy way.

  $ name = 'John Smith'; List ($ first name, $ lastName) = explosion ('', $ name); Echo $ firstName; Echo $ lastName;   

But if you want to keep the account in middle name etc. then

  $ name = 'John Mark Smith'; $ Name = explosion ('', $ name); $ First name = $ name [0]; $ Lastname = (Provider ($ name [count (name $ -1)))? $ Name [count ($ name) -1]: '';   

The second approach will be better, because it will also work for 1 name.

No comments:

Post a Comment