user1599318
user1599318

Reputation: 237

Use preg_match to find word in URL (or string with no spaces)

Sorry I'm kinda new to regex, anyways I have a url like http://example.com/test/folder/?get=ThisisaValue and I need it to return whether the word "get" is in there or not.

Whenever I run the function I get no match? Any help is appreciated thank you.

Sample code, let's say $_SERVER['HTTP_REFERER'] is http://example.com/hello/?state=12345

if(preg_match("state", $_SERVER['HTTP_REFERER'])) {
   echo "match";
} else {
   echo "no match";
}

Upvotes: 1

Views: 3511

Answers (2)

EaterOfCode
EaterOfCode

Reputation: 2222

you are forgetting the delimiters:

preg_match("~state~", $_SERVER['HTTP_REFERER'])

now it should work :)

Upvotes: 1

Mitya
Mitya

Reputation: 34576

I need it to return whether the word "get" is in there or not

In that case you don't need to match anything - you can just check for whether the substring is in the string, using strpos().

if (strpos($url, 'get')) {
    // do something
}

Upvotes: 5

Related Questions