note 75146 added to function.strpos

From: Date: Tue, 15 May 2007 09:21:48 +0000
Subject: note 75146 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-126597@lists.php.net to get a copy of this message
This is a bit more useful when scanning a large string for all occurances between 'tags'. <?php function getStrsBetween($s,$s1,$s2=false,$offset=0) { /*==================================================================== Function to scan a string for items encapsulated within a pair of tags getStrsBetween(string, tag1, <tag2>, <offset> If no second tag is specified, then match between identical tags Returns an array indexed with the encapsulated text, which is in turn a sub-array, containing the position of each item. Notes: strpos($needle,$haystack,$offset) substr($string,$start,$length) ====================================================================*/ if( $s2 === false ) { $s2 = $s1; } $result = array(); $L1 = strlen($s1); $L2 = strlen($s2); if( $L1==0 || $L2==0 ) { return false; } do { $pos1 = strpos($s,$s1,$offset); if( $pos1 !== false ) { $pos1 += $L1; $pos2 = strpos($s,$s2,$pos1); if( $pos2 !== false ) { $key_len = $pos2 - $pos1; $this_key = substr($s,$pos1,$key_len); if( !array_key_exists($this_key,$result) ) { $result[$this_key] = array(); } $result[$this_key][] = $pos1; $offset = $pos2 + $L2; } else { $pos1 = false; } } } while($pos1 !== false ); return $result; } ?> ---- Server IP: 194.145.210.4 Probable Submitter: 81.136.133.55 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/75146 Del: integrated -- https://master.php.net/note/delete/75146/integrated Del: useless -- https://master.php.net/note/delete/75146/useless Del: bad code -- https://master.php.net/note/delete/75146/bad+code Del: spam -- https://master.php.net/note/delete/75146/spam Del: non-english -- https://master.php.net/note/delete/75146/non-english Del: in docs -- https://master.php.net/note/delete/75146/in+docs Del: other reasons-- https://master.php.net/note/delete/75146 Reject -- https://master.php.net/note/reject/75146 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126597) next »