Page 1 of 1
Check if text contains...
Posted: Sat May 07, 2011 6:12 pm
by EcazS
Could someone help me create something that checks if a string contains any of these characters,
\/:*?"<>|
Just point me in the right direction, thanks
(I'm guessing preg?)
Re: Check if text contains...
Posted: Sat May 07, 2011 6:42 pm
by jacek
you could just use preg_match
if (preg_match('#[\\/:\*\?"<>\|]+#', $text) === 1){
// it contains at least one of those.
}
I'm not 100% sure which characters need to be escape in the [] though.
Re: Check if text contains...
Posted: Sat May 07, 2011 6:47 pm
by Kamal
jacek wrote:I'm not 100% sure which characters need to be escape in the [] though.
That depends on what.. umm that # thingy you choose (forgot the name, ill probably find it in php.net).
# is the best for most cases, you can use any character but you must escape it in the regex.
Re: Check if text contains...
Posted: Sat May 07, 2011 6:56 pm
by Tino
I think it's called a hash sign/symbol. In the expression itself it's called an expression delimiter because it tells where the end and begin of the expression is (excluding the optional flag after the second delimiter).
Re: Check if text contains...
Posted: Sat May 07, 2011 7:07 pm
by jacek
Kamal wrote:That depends on what.. umm that # thingy you choose (forgot the name, ill probably find it in php.net).
# is the best for most cases, you can use any character but you must escape it in the regex.
Well yeah, there are some control characters and the # in this case that would need to be escape, I just cant remember which of that list are control characters, I think I had a pretty good guess though,
Re: Check if text contains...
Posted: Sat May 07, 2011 7:10 pm
by Tino
In that list, I think you did. I don't think any of the other characters have any special meaning.
The only one I'm not 100% sure of is the colon.
Re: Check if text contains...
Posted: Sat May 07, 2011 7:48 pm
by EcazS
I'll try it out. If it doesn't work I'll let you know