简体   繁体   中英

PHP RegEx won't pick up question mark

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.

The question mark means that the preceding match is optional, ie

/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?".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM