1

In my SQL, I have some rows that contain email senders. Those senders are strings and not separated via columns.

This is 1 example from my row.

"Test Example" <[email protected]>, "Second Person" <[email protected]>, [email protected]

As you can see, it's plain text and ready for explode. I can explode it with comma and separate them.

But today I realized this example:

"Test, Example" <[email protected]>, "Second Person" <[email protected]>, [email protected]

As you see, Test has a comma in its name. So if I explode it, it will explode Test too. This will cause an error.

I tried some regex but with no luck. Could anyone suggest to me the best regex to explode them but not names?

FYI, names always in "" tag. So commas are not.

I simply need to pass if comma is in quotes.

2 Answers 2

1
explode('>, "', $emails);

And add the exploded chars afterwards - or, better yet, use imap_rfc822_parse_adrlist().

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for imap_rfc822_parse_adrlist(). Probably the best way for doing this.
I like the imap_rfc822_parse_adrlist() solution, but the explode isn't a good idea. If there are spaces between > and , it won't work.
@ColinHebert: FTR, I don't like it either. I just figured if there's a good solution already I shouldn't waste much of my time with a edgy one. The OP should definitively use imap_rfc822_parse_adrlist.
Thanks to everyone! imap_rfc822_parse_adrlist is what i need! Good to know that. Answer is accepted.
0

Perhaps you should be validating your data into the database, so someone's name cannot contain a "," (or other symbols as well).

You dont want your users dictating your formatting display, you should format the text yourself when you need to display it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.