note 73474 modified in function.strstr by danbrown

From: Date: Fri, 28 Nov 2008 20:05:04 +0000
Subject: note 73474 modified in function.strstr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147174@lists.php.net to get a copy of this message
Often you will need to find all occurrences of a string (for security escapes and such) So I wrote this function to return an array with the locations of all the occurrences. Almost like an advanced strstr. <?php function findall($needle, $haystack) { //Setting up $buffer=''; //We will use a 'frameshift' buffer for this search $pos=0; //Pointer $end = strlen($haystack); //The end of the string $getchar=''; //The next character in the string $needlelen=strlen($needle); //The length of the needle to find (speeds up searching) $found = array(); //The array we will store results in while($pos<$end)//Scan file { $getchar = substr($haystack,$pos,1); //Grab next character from pointer if($getchar!="\n" || buffer<$needlelen) //If we fetched a line break, or the buffer is still smaller than the needle, ignore and grab next character { $buffer = $buffer . $getchar; //Build frameshift buffer if(strlen($buffer)>$needlelen) //If the buffer is longer than the needle { $buffer = substr($buffer,-$needlelen);//Truncunate backwards to needle length (backwards so that the frame 'moves') } if($buffer==$needle) //If the buffer matches the needle { $found[]=$pos-$needlelen+1; //Add the location of the needle to the array. Adding one fixes the offset. } } $pos++; //Increment the pointer } if(array_key_exists(0,$found)) //Check for an empty array { return $found; //Return the array of located positions } else { return false; //Or if no instances were found return false } } ?> Haven't had the chance to speed test it, but many optimizations should be possible. It just works enough for me. Hope it saves someone a lot of time. --was-- Often you will need to find all occurrences of a string (for security escapes and such) So I wrote this function to return an array with the locations of all the occurrences. Almost like an advanced strstr. function findall($needle, $haystack) { //Setting up $buffer=''; //We will use a 'frameshift' buffer for this search $pos=0; //Pointer $end = strlen($haystack); //The end of the string $getchar=''; //The next character in the string $needlelen=strlen($needle); //The length of the needle to find (speeds up searching) $found = array(); //The array we will store results in while($pos<$end)//Scan file { $getchar = substr($haystack,$pos,1); //Grab next character from pointer if($getchar!="\n" || buffer<$needlelen) //If we fetched a line break, or the buffer is still smaller than the needle, ignore and grab next character { $buffer = $buffer . $getchar; //Build frameshift buffer if(strlen($buffer)>$needlelen) //If the buffer is longer than the needle { $buffer = substr($buffer,-$needlelen);//Truncunate backwards to needle length (backwards so that the frame 'moves') } if($buffer==$needle) //If the buffer matches the needle { $found[]=$pos-$needlelen+1; //Add the location of the needle to the array. Adding one fixes the offset. } } $pos++; //Increment the pointer } if(array_key_exists(0,$found)) //Check for an empty array { return $found; //Return the array of located positions } else { return false; //Or if no instances were found return false } } Haven't had the chance to speed test it, but many optimizations should be possible. It just works enough for me. Hope it saves someone a lot of time. http://php.net/manual/en/function.strstr.php

« previous php.notes (#147174) next »