Jump to content

Strings, Spaces and Commas?


rarebit

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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