Req #66792 [Opn]: add callback function to preg_match
| From: | requinix@php.net | Date: | Thu, 27 Feb 2014 19:03:19 +0000 |
| Subject: | Req #66792 [Opn]: add callback function to preg_match | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184460@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66792&edit=1
ID: 66792
Updated by: requinix@php.net
Reported by: crayonviolent at phpfreaks dot com
Summary: add callback function to preg_match
Status: Open
Type: Feature/Change Request
Package: PCRE related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Maybe it's just the example but you can easily put that condition in your if. Besides,
doesn't adding a callback make it "unnecessarily complex"?
preg_match('~^(\d+)-(\d+)$~', $string, $match) && $match[1] <= $match[2]
For preg_match_all() you can use PREG_SET_ORDER and array_filter().
Previous Comments:
------------------------------------------------------------------------
[2014-02-27 14:59:42] crayonviolent at phpfreaks dot com
Description:
------------
Pretty straightforward.. Add callback function capability to preg_match and preg_match_all. The
requirements should be to ultimately return true or false. If true then it will count as a match. If
false, it will not count as a match.
This solution would allow injection of arbitrary logic, and I feel this will both greatly enhance
and simplify a lot of common scenarios that benefit from using regex, but are unnecessarily complex
because of limitations of regex, e.g number ranges and dates.
Test script:
---------------
/*
use case:
I expect an integer range from user, and I want to
make sure the 'min' number is less than or equal to the 'max' num
*/
$string = "45-50";
if ( preg_match('~^(\d+)-(\d+)$~', $string, $match,
function($m) { return ($m[1] <= $m[2]); }
) {
// valid
} else {
// invalid
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66792&edit=1