Check if text contains...

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Check if text contains...

Post 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?)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check if text contains...

Post 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.
Image
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Check if text contains...

Post 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.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Check if text contains...

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

:)
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check if text contains...

Post 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,
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Check if text contains...

Post 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.
Please check out my CodeCanyon items.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Check if text contains...

Post by EcazS »

I'll try it out. If it doesn't work I'll let you know :lol: :P
Post Reply