In Java, we can use indexOf
and lastIndexOf
. Since those functions don't exist in PHP, what would be the PHP equivalent of this Java code?
if(req_type.equals("RMT"))
pt_password = message.substring(message.indexOf("-")+1);
else
pt_password = message.substring(message.indexOf("-")+1,message.lastIndexOf("-"));
In php:
stripos() function is used to find the position of the first occurrence of a case-insensitive substring in a string.
strripos() function is used to find the position of the last occurrence of a case-insensitive substring in a string.
You need the following functions to do this in PHP:
Here's the signature of the
substr
function:The signature of the
substring
function (Java) looks a bit different:substring
(Java) expects the end-index as the last parameter, butsubstr
(PHP) expects a length.It's not hard, to get the end-index in PHP:
Here is the working code