alonso Posted January 29, 2009 Share Posted January 29, 2009 Hi! Could you help to split this string with preg_match ? $str ="Name Surname (Comment Variable) <email>" $string1 = "Name Surname" ; $string2 = "Comment Variable" ; $string3= "email" Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/142972-split-my-format-please/ Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 <?php $str ="Name Surname (Comment Variable) <email>"; preg_match('/(.+?)\s+\(([^)]+)\)\s+<([^>]+)>/',$str,$matches); list(,$string1,$string2,$string3) = $matches; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142972-split-my-format-please/#findComment-749687 Share on other sites More sharing options...
alonso Posted January 29, 2009 Author Share Posted January 29, 2009 Thank you very much! and.. what if the "(Comment)" is optional in the string? Quote Link to comment https://forums.phpfreaks.com/topic/142972-split-my-format-please/#findComment-749691 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 preg_match('/(.+?)\s+(?:\(([^)]+)\)\s+)?<([^>]+)>/',$str,$matches); list(,$string1,$string2,$string3) = $matches; Quote Link to comment https://forums.phpfreaks.com/topic/142972-split-my-format-please/#findComment-749702 Share on other sites More sharing options...
alonso Posted January 29, 2009 Author Share Posted January 29, 2009 Thanks again! You are so kind! Quote Link to comment https://forums.phpfreaks.com/topic/142972-split-my-format-please/#findComment-749724 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.