note 127431 added to function.preg-match-all

From: Date: Sat, 06 Aug 2022 19:22:12 +0000
Subject: note 127431 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-219381@lists.php.net to get a copy of this message
Sometimes you don't just want to cherry-pick the matches but need that the entire subject is made up from matching substrings, so every character of the subject is a member of a match. None of the existing preg_* function is easily applicable for this task, so I made the preg_match_entire() function. It uses the (*MARK) syntax which is documented here: https://pcre.org/original/doc/html/pcrepattern.html#SEC27 <?php // returns: the array of matches // null if the string is not a repetition of the pattern // false on error function preg_match_entire( string $pattern, string $subject, int $flags = 0 ){ // Rebuild and wrap the pattern $delimiter = $pattern[0]; $ldp = strrpos( $pattern, $delimiter ); $pattern = substr( $pattern, 1, $ldp - 1 ); $modifiers = substr( $pattern, $ldp + 1 ); $pattern = "{$delimiter} \G\z (*MARK:END) | \G (?:{$pattern}) {$delimiter}x{$modifiers}"; $r = preg_match_all( $pattern, $subject, $m, PREG_SET_ORDER | $flags ); if( $r === false ) return false; // error $end = array_pop( $m ); if( $end === null || ! isset( $end['MARK']) || $end['MARK'] !== 'END') return null; // end of string not reached return $m; // return actual matches, may be an empty array } // Same results: test('#{\d+}#', ''); // [] test('#{\d+}#', '{11}{22}{33}'); // {11},{22},{33} // Different results: preg_match_entire won't match this: test('#{\d+}#', '{11}{}{aa}{22},{{33}}'); // preg_match_entire: null // preg_match_all: {11},{22},{33} function test( $pattern, $subject ){ echo "pattern: $pattern\n"; echo "subject: $subject\n"; print_matches('preg_match_entire: ', preg_match_entire( $pattern, $subject )); preg_match_all( $pattern, $subject, $matches, PREG_SET_ORDER ); print_matches('preg_match_all: ', $matches ); echo "\n"; } function print_matches( $t, $m ){ echo $t, is_array( $m ) && $m ? implode(',', array_column( $m, 0 )) : json_encode( $m ), "\n"; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 84.236.34.174) ---- Manual Page -- https://php.net/manual/en/function.preg-match-all.php Edit -- https://main.php.net/note/edit/127431 Del: integrated -- https://main.php.net/note/delete/127431/integrated Del: useless -- https://main.php.net/note/delete/127431/useless Del: bad code -- https://main.php.net/note/delete/127431/bad+code Del: spam -- https://main.php.net/note/delete/127431/spam Del: non-english -- https://main.php.net/note/delete/127431/non-english Del: in docs -- https://main.php.net/note/delete/127431/in+docs Del: other reasons-- https://main.php.net/note/delete/127431 Reject -- https://main.php.net/note/reject/127431 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219381) next »