Reputation: 97
I am trying to match urls in a string using the PHP function "preg_match_all". It works fine, except it will not match urls with question marks in them.
For example, this will match fine:
http://espn.com/mlb
But this will not match:
http://espn.com/mlb?player=71
Here is the regex I am using,
$regexUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
I cannot figure out why the question mark is not being picked up by the \S. I've tried a lot of different expressions and cannot get the question mark to match. Any ideas?
EDIT:
It turns out preg_match_all was returning true, but I was not escaping the question mark in the preg_match_all output, so the preg_replace call that I was making later on was failing.
Upvotes: 1
Views: 3984
Reputation: 4176
The question mark means that the preceding match is optional, i.e.
/https?/
will cause both "http" and "https" to match. You must escape the question mark to match it.
For example:
/https\?/
will now only match "https?".
Upvotes: 5