Jump to content

[SOLVED] How to cut a string into multiple variables?


lopes_andre

Recommended Posts

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é.

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...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.