Page 1 of 1
preventing certain BBCode tags in form
Posted: Sat Apr 25, 2009 9:44 am
by oboedrew
I'm trying to prevent the use of certain BBCode tags in certain fields of a form using this code:
if(preg_match('|\[quote]|\[/quote]|\[i]|\[/i]|\[url]|\[url=.+]|\[/url]|', $field)){
redisplays form with error message
}
But this doesn't work. I can still submit the form with these BBCode tags. Can anyone explain this? Is there something wrong with my regex, perhaps?
Thanks,
Drew
Re: preventing certain BBCode tags in form
Posted: Sat Apr 25, 2009 1:25 pm
by McInfo
Try changing the first and last pipes (|) in your pattern to some other character, like #.
Edit: This post was recovered from search engine cache.
Re: preventing certain BBCode tags in form
Posted: Sat Apr 25, 2009 5:22 pm
by oboedrew
Yep, that worked. I had already tried switching the outer |'s to /'s, and that had not worked, so I had concluded the delimiter was not the problem. But now I see I was mistaken. Is it safe to assume | and / were not working as delimiters because they are both used in the regex? If so, I suppose it's best to get into a habit of always using something like # or @ or % that's not likely to appear in a regex.
Thanks,
Drew
Re: preventing certain BBCode tags in form
Posted: Sat Apr 25, 2009 5:31 pm
by s.dot
Your problem was you were using the pipe character in the regex and as a delimiter.. preg_match thought your second | was the ending delimiter.
I usually use / as a delimiter but sometimes # (because urls contain ://.. it gets a bit tricky).
You can also preg_quote() your regex pattern (using the optional second argument as your delimiter) to avoid this type of problem.. but that is usually reserved for when the patterns are unknown.