php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40182 Want a function strpos_all with optional parameters
Submitted: 2007-01-20 19:26 UTC Modified: 2017-03-14 23:59 UTC
Votes:2
Avg. Score:2.5 ± 1.5
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: krudtaa at yahoo dot com Assigned: pollita (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.0 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: krudtaa at yahoo dot com
New email:
PHP Version: OS:

 

 [2007-01-20 19:26 UTC] 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!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-23 13:16 UTC] narf at devilix dot net
-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;
}
 [2017-03-14 23:59 UTC] pollita@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: pollita
 [2017-03-14 23:59 UTC] pollita@php.net
As noted, this can be trivially accomplished in userspace. Therefore it should be accomplished in userspace.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jun 24 10:01:35 2025 UTC