note 71513 deleted from function.strpos by danbrown

From: Date: Sun, 17 May 2009 16:33:50 +0000
Subject: note 71513 deleted from function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154848@lists.php.net to get a copy of this message
Note Submitter: Charles ---- Small improvement on the efforts of others: <?php function strpos_all($hs_haystack, $hs_needle, $hn_offset = 0, $hn_limit = 0) { $ha_positions = array(); $hn_count = 0; while (false !== ($pos = strpos($hs_haystack, $hs_needle, $hn_offset)) && ($hn_limit == 0 || $hn_count < $hn_limit)) { $ha_positions[] = $pos; $hn_offset = $pos + strlen($hs_needle); ++$hn_count; } return $ha_positions; } function preg_pos($hs_pattern, $hs_subject, &$hs_foundstring, $hn_offset = 0) { $hs_foundstring = NULL; if (preg_match($hs_pattern, $hs_subject, $ha_matches, PREG_OFFSET_CAPTURE, $hn_offset)) { $hs_foundstring = $ha_matches[0][0]; return $ha_matches[0][1]; } else { return FALSE; } } function preg_pos_all($hs_pattern, $hs_subject, &$ha_foundstring, $hn_offset = 0, $hn_limit = 0) { $ha_positions = array(); $ha_foundstring = array(); $hn_count = 0; while (false !== ($pos = preg_pos($hs_pattern, $hs_subject, $hs_foundstring, $hn_offset)) && ($hn_limit == 0 || $hn_count < $hn_limit)) { $ha_positions[] = $pos; $ha_foundstring[] = $hs_foundstring; $hn_offset = $pos + 1; // alternatively: '$pos + strlen($hs_foundstring)' ++$hn_count; } return $ha_positions; } print_r(preg_pos_all('/s...s/', "she sells sea shells on the sea floor", $ha_matches)); print_r($ha_matches); ?>

« previous php.notes (#154848) next »