Checking if a ? is in a variable...

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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Checking if a ? is in a variable...

Post by Mr Tech »

I tried to use this code to check and see if a question mark (?) is in a $variable...

Code: Select all

if (!preg_match("/?/", $back_page)) {
However, it doesn't work... What can I do to make it work?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

? is a regexp character. Try:

Code: Select all

if (!preg_match("/[\?]/", $back_page)) {
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

From the PHP Manual on preg_match:
The PHP Manual: preg_match page wrote:Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

arborint wrote:? is a regexp character. Try:

Code: Select all

if (!preg_match("/[\?]/", $back_page)) {

Code: Select all

if (!preg_match('/'.preg_quote('?','/').'/', $back_page)) {
:twisted:
Post Reply