|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-27 14:59 UTC] 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
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 12:00:02 2025 UTC |
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().