preventing certain BBCode tags in form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

preventing certain BBCode tags in form

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: preventing certain BBCode tags in form

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 12:17 pm, edited 1 time in total.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: preventing certain BBCode tags in form

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: preventing certain BBCode tags in form

Post 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.
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.
Post Reply