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 8156155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:51:57+00:00 2026-06-06T16:51:57+00:00

I have an array of patterns: $patterns= array( ‘#http://(www\.)?domain-1.com/.*#i’, ‘#http://(www\.)?domain-2.com/.*#i’, … ); and I

  • 0

I have an array of patterns:

$patterns= array(
    '#http://(www\.)?domain-1.com/.*#i',
    '#http://(www\.)?domain-2.com/.*#i',
    ...
);

and I have long string that contains multiple text and urls I want to match the first url occurred in the string, I only tried this :

foreach ($patterns as $pattern) {
    preg_match($pattern, $the_string, $match);
    echo '<pre>'; print_r($match); echo '</pre>';
}

It returns empty arrays where there are no match for some patterns and arrays that contains a url but depending on the order of the array $patterns,

how can I find any match of these patterns that occurred first.

  • 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-06T16:51:59+00:00Added an answer on June 6, 2026 at 4:51 pm

    You basically have three options:

    1. match a general URL pattern and then run that URL against the patterns you’ve got. If none matched, continue with the second result from the general pattern.
    2. run all your patterns with the PREG_OFFSET_CAPTURE flag to get the offset the pattern matched at. find the lowest offset, return your result
    3. combine your various patterns to a single pattern. Be aware that there are limits to the length of a pattern (64K in compiled form)

    Option 2:

    <?php
    
    $text = "hello world http://www.domain-2.com/foo comes before http://www.domain-1.com/bar";
    $patterns= array(
        '#http://(www\.)?domain-1.com/[^\s]*#i',
        '#http://(www\.)?domain-2.com/[^\s]*#i',
    );
    
    $match = null;
    $offset = null;
    foreach ($patterns as $pattern) {
        if (preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE)) {
            if ($matches[0][1] < $offset || $offset === null) {
                $offset = $matches[0][1];
                $match = $matches[0][0];
            }
        }
    }
    
    var_dump($match);
    

    beware that I changed your demo patterns. I replaced .* (anything) by [^\s]* (everything but space) to prevent the pattern from matching more than it’s supposed to

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

Sidebar

Related Questions

I have a very large set of string URL patterns like {http://www.imdb.com, http://www.amazon.com ,...}
I have an array full of patterns that I need matched. Any way to
So I started reading this book: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022 On chapter 2 it explains about the
I have a string that contains something like this: ##### abc 'foo' /path/to/filename:1 #####
I have array of strings, String[] data and it's 10 elements has value P
I have some problems using two dimensional array. static const int PATTERNS[20][4]; static void
I have a string that I would like to replace with another one if
I have an array of objects that have 2 properties: Key and Value. I
I have Date that look like String, and I want parse it. But date
I have a 2D array of random characters. I want to match specific patterns

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.