lopes_andre Posted March 15, 2009 Share Posted March 15, 2009 Hi, I have a form that accepts email addresses separated by ";", like this: [email protected]; [email protected], [email protected] I will use PHPMailer to send this emails, and I need to cut the "[email protected]; [email protected]; [email protected]", into this: $mail1 = "[email protected]"; $mail2 = "[email protected]"; $mail2 = "[email protected]"; How can I solve this problem? Wich is the best solution? Ps: Sorry my english. Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/149580-solved-how-to-cut-a-string-into-multiple-variables/ Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 so you want to split at semicolons or commas? Quote Link to comment https://forums.phpfreaks.com/topic/149580-solved-how-to-cut-a-string-into-multiple-variables/#findComment-785465 Share on other sites More sharing options...
lopes_andre Posted March 15, 2009 Author Share Posted March 15, 2009 I need to split on ";" Sorry by the confusion on last post. Quote Link to comment https://forums.phpfreaks.com/topic/149580-solved-how-to-cut-a-string-into-multiple-variables/#findComment-785468 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 explode will explode the string at the semicolons, and put each item into an array. Example: $string = "[email protected]; [email protected]; [email protected]"; $email = explode(";",$string); However, if there is also a space after the semicolon, each email is going to have a leading space. If you are 100% sure that there will always be a space, you can include the space in the explode like so: $email = explode("; ",$string); If you are not 100% sure (as in, it may or may not have a space), you can either loop through the array and trim each element, or you can instead, use preg_split, making it optionally match the space, like so: $email = preg_split('~;\s?~',$string); Understand that all of these methods makes an array, so does not make like $email1, $email2, $email3, etc... it makes $email[0], $email[1], $email[2], etc... Quote Link to comment https://forums.phpfreaks.com/topic/149580-solved-how-to-cut-a-string-into-multiple-variables/#findComment-785477 Share on other sites More sharing options...
lopes_andre Posted March 15, 2009 Author Share Posted March 15, 2009 Thanks a lot! It works! Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/149580-solved-how-to-cut-a-string-into-multiple-variables/#findComment-785485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.