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
preventing certain BBCode tags in form
Moderator: General Moderators
Re: preventing certain BBCode tags in form
Try changing the first and last pipes (|) in your pattern to some other character, like #.
Edit: This post was recovered from search engine cache.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 12:17 pm, edited 1 time in total.
Re: preventing certain BBCode tags in form
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
Thanks,
Drew
Re: preventing certain BBCode tags in form
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.
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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.