Req #52124 [Com]: [strpos] Limit search inside $haystack by new $length parameter

From: Date: Tue, 28 Jul 2015 16:01:44 +0000
Subject: Req #52124 [Com]: [strpos] Limit search inside $haystack by new $length parameter
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194783@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52124&edit=1

 ID:                 52124
 Comment by:         vovan-ve at yandex dot ru
 Reported by:        vovan-ve at yandex dot ru
 Summary:            [strpos] Limit search inside $haystack by new
                     $length parameter
 Status:             Open
 Type:               Feature/Change Request
 Package:            Strings related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2015-07-26 17:53:12] cmb@php.net

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;
    }

------------------------------------------------------------------------
[2010-06-19 12:13:02] vovan-ve at yandex dot ru

Description:
------------
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
  var_dump($pos);

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);
var_dump($pos);

Expected result:
----------------
int(26)



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=52124&edit=1


Thread (4 messages)

« previous php.bugs (#194783) next »