note 107111 deleted from function.preg-match by cmb

From: Date: Sun, 13 Oct 2019 07:55:00 +0000
Subject: note 107111 deleted from function.preg-match by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212619@lists.php.net to get a copy of this message
Note Submitter: kevin ---- here is a little function to get an associative array instead of the numeric one. <?php function preg_match_assoc($pattern, $subject, $assoc, $flags = 0, $offset = 0) { $matches = array(); eval('preg_match($pattern, $subject, $matches, $flags, $offset);'); $n = 0; foreach($matches as $result) { $array[$assoc[$n]] = $result; $n++; } return $array; } ?> example of use <?php $assocs = array( 'all', 'a-1', 'i-1', 'a-2', 'ia-1', 'ia-2' ); $test = preg_match_assoc('#([a-z]+)([0-9]+)([a-z]+)\-([a-z|0-9]+)\-([a-z|0-9]+)#', 'az45rt-df36qz-fg89ih', $assocs); // $test will contain : // // array // 'all' => string 'az45rt-df36qz-fg89ih' (length=20) // 'a-1' => string 'az' (length=2) // 'i-1' => string '45' (length=2) // 'a-2' => string 'rt' (length=2) // 'ia-1' => string 'df36qz' (length=6) // // Instead of : // // array // 0 => string 'az45rt-df36qz-fg89ih' (length=20) // 1 => string 'az' (length=2) // 2 => string '45' (length=2) // 2 => string 'rt' (length=2) // 4 => string 'df36qz' (length=6) // 5 => string 'fg89ih' (length=6) ?>

« previous php.notes (#212619) next »