Req #40182 [Com]: Want a function strpos_all with optional parameters
Edit report at https://bugs.php.net/bug.php?id=40182&edit=1
ID: 40182
Comment by: narf at devilix dot net
Reported by: krudtaa at yahoo dot com
Summary: Want a function strpos_all with optional parameters
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: all
PHP Version: 5.2.0
Block user comment: N
Private report: N
New Comment:
-1:
function strpos_all($haystack, $needle)
{
$needle = preg_quote($needle, '#');
return preg_match_all("#{$needle}#", $haystack, $matches, PREG_OFFSET_CAPTURE)
? array_column($matches[0], 1)
: false;
}
Previous Comments:
------------------------------------------------------------------------
[2007-01-20 19:26:32] krudtaa at yahoo dot com
Description:
------------
I would very much like to see a function that returns all positions of a needle within a haystack.
Similar to strpos but named to strpos_all
And should return an array with all positions of needle within haystack.
Should also be able to take at least two optional parameters as well:
one arithmetic operator
one number
see below for explanation of why and motivation.
My specific need is to be able to search a string 5000 chars in length which consist of only zeroes
and ones (0 or 1).
And I want to get position of all chars set to 1.
I read through the comments on www.php.net/strpos and the closest solution I could find to what I
wanted was this:
$bitvec = str_repeat('1',5000);
$posArr = array();
// next two lines from www.php.net/strpos comments
$pos=-1; // need this to start on first element
while ( ($pos = strpos($bitvec4,'1',$pos+1)) !== false ) $posArr[]=$pos ;
The above is worst case. Normally, for my use, the bitvector, $bitvec, would not have all chars set
to 1, but it will happen.
Using the above code to get all the positions where char = 1 is so much slower compared to if
someone could write a function that handles this.
IMHO I think this is something many could use.
To make this "new" function "strpos_all" even better,
if possible, I would like it to be able to pass two other values:
one number and one arithmetic operator
If these are set then I want the value to be stored in
the array element to be (if arithmetic operator was *)
Position * number
If the new function could NOT do this then, at least I, would have to iterate through all the
elements to change the values which again would take a "long" time if done on each element
in PHP compared to if done within the "upcoming"
strpos_all function.
Really hope to see a function that can do this.
My goal is to use PHP and database to build a fast
Seach Engine using a combination of "vectorspace" and "reversed index".
In short I want to do what TBGsearch does with PERL and MYSQL, but using PHP and MYSQL:
http://www.tbg.nu/tbgsearch/
The one thing so far that slow my "PHP solution" to much down is the lack of strpos_all
string with my suggested optional parameters (one artithmetic operator and one number).
Keep up the good work!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=40182&edit=1
Thread (3 messages)