rarebit Posted February 1, 2009 Share Posted February 1, 2009 Hi, I have the following which extracts quoted strings. I can also split on spaces or commas, but I can't seem to do it all at once. $string = "joe 'it is' admin"; preg_match_all('/\B(["\'])(.+?)\1\B/s', $string, $matches); print_r($matches); Any ideas please? Cheers! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 1, 2009 Share Posted February 1, 2009 Maybe I'm just not getting what you are trying to do, as you are using the term 'split', which makes me think of preg_split()...yet in your example, you are capturing anything between double or single quotes.. (which from the sounds of it, you would want to print_r($matches[2])). Could you give some more small example strings of conditions you are looking at and what results you are trying to achieve? Quote Link to comment Share on other sites More sharing options...
rarebit Posted February 3, 2009 Author Share Posted February 3, 2009 Your right preg_split is probably more of what I want, I was trying to use brackets, but... Anyway this is where i've got: $string = "this isn't an \"interesting example\", but 'then what' would we expect?"; //$string = "I saw 'em 'ther day like 'had bells' on they did \"that arh\" y'know! But 'hey what's thee' expect like?"; print $string."<br>"; $matches = preg_split('/\B(["\'])(.+?)\1\B|[\s,]+/s', $string, 0, PREG_SPLIT_DELIM_CAPTURE ); print_r($matches); This is more than adequate, even though it also enters the opening quote into the resulting array. Basically what it is, is I have formulated an email type form which has a buddy list. The 'to' list was split on spaces or commas. General user names are not allowed spaces, but for a project I have also added business names to the mix (using spaces). Knowing that I can wrap business names with some form of quotes when building the buddy list or doing a reply, forward, etc. However if the user types in the business name directly without quotes then some kind of unknown user handler will return. So after consideration, i'm thinking of changing the whole lot and using a single standard delimiter of ';', and just banning it's use in either usernames or business names... Whatcha think? Cheers! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 Isn't email listings normally delimited by ; or ,? Are you scanning for email addresses / business names in a string of text? Quote Link to comment Share on other sites More sharing options...
rarebit Posted February 3, 2009 Author Share Posted February 3, 2009 mmm, I was wanting to do something a little more open, and no it's just an internal messaging system. To start with it was fine just using spaces, tabs, commas an' such. But then names which contained spaces were introduced, so it was decided that they'd need to be quoted, but hey it's surely going to be easier for me and for the general user to understand by using a general list separator, such as the semi-colon. Even though the above regex does the job, it's not really covered by the "don't make me think" philosophy... Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 Yeah, problem is spaces are too common to be used as delimiters..I would enforce double quotes by example, then preg_split it and search for entries with double quotes... Quote Link to comment 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.