Jump to content

[SOLVED] Preg_split - Filtering the "." from strings


Recommended Posts

It's not working because I didn't explain myself right, I'm sorry. :-[

 

I want to split strings and use multiple delimiters, not just the . (period) I want to filter:

. - _ ,? ! and white space (" ").

 

I was using

$terms = preg_split("/[\s,]+/", $kernel->vars['string']);

 

But it didn't filter the "." and I don't understand the delimiters character meaning.

 

What are the right delimiters for filtering the above characters?

From what I know, and I could be wrong, the regex for those characters would be:

 

"/[\s][\.][\-][\_][\,][\?][\!]/"

 

There may be an easier way to do this and like I said, it could be wrong. Only one way to find out.....

 

Hope that helps.

You should be able to group those into one character set:

 

"/[\s\.-_,?!]/"

 

Be carefule with the location of the - character in your character class... if it is not the first or last character, this character now signifies a range (instead of a dash). Best to relocate that character..also, you do not need to escape the dot in this case, as it is automatically recognized as a dot (and not a wildcard) character when placed within a character class:

 

"/[\s.,_?!-]/"

 

EDIT - To the OP.. \s is a shorthand character class that encompasses numerous spaces, from individual ones to tabs and returncarriages by example..if you ONLY want an actual space, you can replace \s with a single literal space, or use \x20

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.