Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:52:06+00:00 2026-06-01T21:52:06+00:00

I am trying to match urls in a string using the PHP function preg_match_all.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:52:07+00:00Added an answer on June 1, 2026 at 9:52 pm

    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?”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a python regular expression that will match both these URLs:
I am trying to match a string only if it is not part of
I'm trying to match the following items in the string pcode : u followed
I'm trying to match chunks of JS code and extract string literals that contain
I am trying to match a series of text strings with PCRE on PHP,
I'm trying to match URLs with wildcards in them to actual URLs. For example:
I'm trying to match any requests for a url that do not have a
I am trying to write a function that pulls all url's from a string
I am trying to create a url router in PHP, that works like django's.
I'm having a very basic question about regular expressions. I am trying to match

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.