transparencia Posted December 14, 2008 Share Posted December 14, 2008 How can I filter the "." (period) when splitting strings using preg_split? Greetings! Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/ Share on other sites More sharing options...
wildteen88 Posted December 14, 2008 Share Posted December 14, 2008 With preg_split you'll have to escape it, eg: \. The period (.) in regex has a special meaning, escaping it tells the PCRE engine to treat it as literal. Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715225 Share on other sites More sharing options...
transparencia Posted December 14, 2008 Author Share Posted December 14, 2008 How can I do that? Does it need another function? The string splitting is for a search engine, perfomance is a must. PS: Thanks for your anwser. Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715230 Share on other sites More sharing options...
wildteen88 Posted December 14, 2008 Share Posted December 14, 2008 Umm, I just said. You escape it like so: \. Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715231 Share on other sites More sharing options...
transparencia Posted December 14, 2008 Author Share Posted December 14, 2008 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? Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715240 Share on other sites More sharing options...
Mark Baker Posted December 14, 2008 Share Posted December 14, 2008 Unless you have a very special reason why you must use preg_split(), why not use str_word_count() instead Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715282 Share on other sites More sharing options...
DamienRoche Posted December 14, 2008 Share Posted December 14, 2008 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. Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715287 Share on other sites More sharing options...
wildteen88 Posted December 14, 2008 Share Posted December 14, 2008 You should be able to group those into one character set: "/[\s\.-_,?!]/" Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715290 Share on other sites More sharing options...
nrg_alpha Posted December 14, 2008 Share Posted December 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715294 Share on other sites More sharing options...
transparencia Posted December 15, 2008 Author Share Posted December 15, 2008 Thank you all. Works great with "/[\s.,_?!-]/". Solved! Link to comment https://forums.phpfreaks.com/topic/136945-solved-preg_split-filtering-the-from-strings/#findComment-715461 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.