|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52124 [strpos] Limit search inside $haystack by new $length parameter
Submitted: 2010-06-19 12:13 UTC Modified: 2015-07-26 17:53 UTC
Avg. Score:3.8 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:3 (60.0%)
From: vovan-ve at yandex dot ru Assigned:
Status: Open Package: Strings related
PHP Version: Irrelevant OS:
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: vovan-ve at yandex dot ru
New email:
PHP Version: OS:


 [2010-06-19 12:13 UTC] vovan-ve at yandex dot ru
Why strpos() does not accept $length parameter with addition to $start? It would be very useful to search a $needle inside a part of $haystack sometimes. Now such behaviour can be made by combination with substr():

  $string = 'The big string [with some data here] and with more data there';
  // searh inside substring from offset 16 with length 19
  $tmp = substr($string, 16, 19);
  // now real search
  $pos = strpos($tmp, 'data');
  if (false !== $pos) $pos += 16; // original position

It work, but not optimal due to redundant copy of substring. I don't need this copy, I just want to reduce searching area.

Test script:
$string = 'The big string [with some data here] and with more data there';
// search 'data' inside substring from offset 16 with length 19
$pos = strpos($string, 'data', 16, 19);

Expected result:


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-26 17:53 UTC]
I have some doubts that this functionality is required often
enough to warrant adding it to PHP, considering that it can easily
be implemented in userland (without making a copy of the string):

    function strpos_len($haystack, $needle, $offset, $length) {
        $pos = strpos($haystack, $needle, $offset);
        if ($pos > $offset + $length - strlen($needle)) {
            $pos = false;
        return $pos;
 [2015-07-28 16:01 UTC] vovan-ve at yandex dot ru
Yes, it can be implemented in userland, but in that manner it will scan whole string upto the very end instead of early fail.

It's very rate sutiation where the FR is really needed with respect to tonns of data per millisecond. Just ignore this FR.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Mar 05 04:01:23 2021 UTC