php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55179 Offer preg_match with array-support for pattern
Submitted: 2011-07-11 07:38 UTC Modified: 2020-02-02 22:38 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: neufeind@php.net Assigned:
Status: Suspended Package: PCRE related
PHP Version: 5.3.6 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: neufeind@php.net
New email:
PHP Version: OS:

 

 [2011-07-11 07:38 UTC] neufeind@php.net
Description:
------------
Maybe the existing preg_match could allow for an array to be given as $pattern. Or if not a preg_match_array() might be an alternative.

Idea is to check a string against a list of pattern. First matched pattern would return.

Test script:
---------------
<?php
  $patterns = array(
    '/\.link1$/',
    '/\.link2$/',
    '/\.link3$/',
    '/\.link4$/',
  );

  $subject = "field.link2";

  if(preg_match($patterns, $subject)) {
    // ...
  }


Expected result:
----------------
preg_match should find the first pattern that matches (if any).

Actual result:
--------------
No array-support for $pattern yet.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-02 22:38 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-02-02 22:38 UTC] cmb@php.net
Well, it seems to me that you basically could pass

    implode('|', $patterns)

to preg_match(); that would need slightly modified $patterns, and
preg_quote() for the general case, though.  And of course, the
given example could use a single regex with a character class.

In my opinion, the feature as presented is not very useful, and
under-specified (for instance, how to detect which pattern
matched), and as such needs discussion on the internals mailing
list[1].  Please feel free to propose the feature there.  For the
time being, I'm suspending this ticket.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 06:01:30 2025 UTC