nooblag
nooblag

Reputation: 349

How to check for a question mark in PHP string using regex?

I have a string returning a URL and need to figure out how to use if and regex to do something whenever the returned URL contains a question mark.

So when $url is http://somedomain.com/?p=34325&blahblahblah

IF the $url contains "?p=" (and discarding everything else) then exit, otherwise do something


Any help?
Many thanks

Upvotes: 4

Views: 10179

Answers (3)

AlexP
AlexP

Reputation: 9857

if (preg_match("/(\?p=)/", $url)) {

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

You don't need a regex to match a simple string:

if (strpos($url, '?p=') !== false) {
   exit;
}
//do something

If you really wanted to use a regex:

if (preg_match('/\?p=/', $url)) {

Upvotes: 9

Paul
Paul

Reputation: 141827

Don't use regex for this. You do not need the power of regular pattern matching. Just use strstr:

if(strstr($url, 'p=?')){
    exit;
}

// Do other stuff

Upvotes: 4

Related Questions