dotancohen Posted November 16, 2006 Share Posted November 16, 2006 I'm parsing a body of text for tags that use square brackets to identify themselves, like BBCode. These tags may contain alphanumeric characters, spaces, apostrophes, underscores, dashes, basic punctuation, and pipes. The closest that I've been able to come up with is:[code] $text=preg_replace_callback('/\[([A-Za-z0-9\¦\'.-:underscore:]+)\]/i' , "findLinks", $text); [/code]However, this does not match spaces for some odd reason (the "." should match them, I think). I've added "\s", "\w", " ", and ":space:" to the regex (tried both before the A-Z and after the 0-9) but for whatever reason those spaces are not detected. Why? What must I do?I'm certain that what I think is a space is most certainly a space. str_replace(" ", "", $text); closes the spaces, so I know that php does in fact see it as a space.I'm on php 5.x if it's relevant. Thanks in advance for any assistance.Dotan Cohenhttp://what-is-what.com Link to comment https://forums.phpfreaks.com/topic/27527-regex-not-matching-spaces/ Share on other sites More sharing options...
effigy Posted November 17, 2006 Share Posted November 17, 2006 Inside a character class a hyphen is a metacharacter that creates a range. I'm guess that[tt] .-: [/tt]is being processed as "the characters from period to colon" instead of "a period, a hyphen, a colon." It's recommended that if you want to match a hyphen, that it be the first character in the class. Where did[tt] :underscore: [/tt]come from? Link to comment https://forums.phpfreaks.com/topic/27527-regex-not-matching-spaces/#findComment-126153 Share on other sites More sharing options...
bljepp69 Posted November 17, 2006 Share Posted November 17, 2006 [code]\s[/code] is what you need to identify a space. Also, you need to escape the period(.) in your regex. So, try something like[code]$text=preg_replace_callback('/\[([A-Za-z0-9\¦\'\.-_\s]+)\]/i' , "findLinks", $text); [/code] Link to comment https://forums.phpfreaks.com/topic/27527-regex-not-matching-spaces/#findComment-126247 Share on other sites More sharing options...
dotancohen Posted November 19, 2006 Author Share Posted November 19, 2006 Thanks. I didn't escape the period because I want the period to match _any_ character, including spaces! Link to comment https://forums.phpfreaks.com/topic/27527-regex-not-matching-spaces/#findComment-127095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.