note 125474 added to function.strrpos
| From: | anonymous@php.net | Date: | Thu, 29 Oct 2020 15:46:13 +0000 |
| Subject: | note 125474 added to function.strrpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215673@lists.php.net to get a copy of this message | ||
There is a lot of confusion around how $offset works and I feel it's really quite simple.
If $offset is positive, the operation ignores the first $offset characters of the haystack.
If $offset is negative, the operation ignores the last $offset-1 characters of the haystack
(don't ask why -1...).
To understand this instinctively, just imagine the characters being replaced with invalid symbols.
Here's an example:
<?php
$hackstack = '0123456789';
// Search the entire sequence 0123456789
var_dump(strrpos($hackstack, '0')); // int(0)
var_dump(strrpos($hackstack, '9')); // int(9)
// Only search ##23456789
var_dump(strrpos($hackstack, '1', 2)); // bool(false)
var_dump(strrpos($hackstack, '2', 2)); // int(2)
// Only search 0123456###
var_dump(strrpos($hackstack, '6', -4)); // int(6)
var_dump(strrpos($hackstack, '7', -4)); // bool(false)
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.174 (proxied: 49.12.160.16)
----
Manual Page -- http://php.net/manual/en/function.strrpos.php
Edit -- https://master.php.net/note/edit/125474
Del: integrated -- https://master.php.net/note/delete/125474/integrated
Del: useless -- https://master.php.net/note/delete/125474/useless
Del: bad code -- https://master.php.net/note/delete/125474/bad+code
Del: spam -- https://master.php.net/note/delete/125474/spam
Del: non-english -- https://master.php.net/note/delete/125474/non-english
Del: in docs -- https://master.php.net/note/delete/125474/in+docs
Del: other reasons-- https://master.php.net/note/delete/125474
Reject -- https://master.php.net/note/reject/125474
Search -- https://master.php.net/manage/user-notes.php