Orionsbelter Posted February 20, 2010 Share Posted February 20, 2010 Hi there i stuck and wondering it is is possible to break up a string into two where there is a space for example $fullName="John Doe"; i wish to break the string up into $firstName="John"; $lastName="Doe"; I need the script to break the two names up from the space it can not contain the space such as $firstName="John "; $lastName=" Doe"; It must split it in two and remove that white space. Pleas help me i'm sure it must be able to do this. Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/ Share on other sites More sharing options...
sader Posted February 20, 2010 Share Posted February 20, 2010 list($first, $last) = explode(" ", $str); Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/#findComment-1015317 Share on other sites More sharing options...
Orionsbelter Posted February 20, 2010 Author Share Posted February 20, 2010 will that not put two strings together? i wish to take one part Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/#findComment-1015322 Share on other sites More sharing options...
sader Posted February 20, 2010 Share Posted February 20, 2010 explode() splits string into peaces by seperator implode() joins array of string/nums into one string by given glue. If u ineterested what list does read about it in manual. I could do it the same with this, but a bit more code is required $person_data = explode(" ", $str); $first = $person_data[0]; $last = $person_data[1]; Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/#findComment-1015326 Share on other sites More sharing options...
salathe Posted February 20, 2010 Share Posted February 20, 2010 Depending on how restricted the input is, you'll probably want to use the limit parameter for explode as well: // Limit to two parts so "John Smith Jnr" becomes "John" and "Smith Jnr" list($first, $last) = explode(" ", $fullName, 2); Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/#findComment-1015327 Share on other sites More sharing options...
Orionsbelter Posted February 20, 2010 Author Share Posted February 20, 2010 Thank you both of you this is very good and i will learn this so i understand the code Link to comment https://forums.phpfreaks.com/topic/192741-break-up-string-into-two-when-there-is-a-space/#findComment-1015332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.