Php string tokenizing?

Use explode() with the optional limit param: $full_name = "John Roberts-Smith II" // Explode at most 2 elements $arr = explode(' ', $full_name, 2); // Your values: $arr0 = "John" $arr1 = "Roberts-Smith II Your last case is special though, placing the first name into the second array element. That requires special handling: If the name contains no whitespace, // put the whole thing in the second array element. If (!strpos($full_name, ' ')) { $arr0 = ''; $arr1 = $full_name; } So a complete function: function split_name($name) { if (!strpos($name, ' ')) { $arr = array(); $arr0 = ''; $arr1 = $name; } else $arr = explode(' ', $name, 2); return $arr; }.

Use explode() with the optional limit param: $full_name = "John Roberts-Smith II" // Explode at most 2 elements $arr = explode(' ', $full_name, 2); // Your values: $arr0 = "John" $arr1 = "Roberts-Smith II" Your last case is special though, placing the first name into the second array element. That requires special handling: // If the name contains no whitespace, // put the whole thing in the second array element. If (!strpos($full_name, ' ')) { $arr0 = ''; $arr1 = $full_name; } So a complete function: function split_name($name) { if (!strpos($name, ' ')) { $arr = array(); $arr0 = ''; $arr1 = $name; } else $arr = explode(' ', $name, 2); return $arr; }.

Thanks Michael. I will try this example. Appreciate your help – user657514 Jul 29 at 14:33 @user657514 be sure to change the $full_name to $name - I just fixed the typo above.

– Michael Jul 29 at 14:43.

You should explode() function for this purpose. $name_splitted = explode(" ", "John Smith", 2); echo $name_splitted0; // John echo $name_splitted1; // Smith From the documentation - array explode ( string $delimiter , string $string , int $limit ) Returns an array of strings, each of which is a substring of "string" formed by splitting it on boundaries formed by the string "delimiter". If "limit" is set and positive, the returned array will contain a maximum of "limit" elements with the last element containing the rest of string.

If the "limit" parameter is negative, all components except the last -"limit" are returned. If the "limit" parameter is zero, then this is treated as 1.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions