indexOf and lastIndexOf in PHP? -
in java, can use indexof
, lastindexof
. since functions don't exist in php, php equivalent of java code?
if(req_type.equals("rmt")) pt_password = message.substring(message.indexof("-")+1); else pt_password = message.substring(message.indexof("-")+1,message.lastindexof("-"));
you need following functions in php:
strpos
find position of first occurrence of substring in string
strrpos
find position of last occurrence of substring in string
substr
return part of string
here's signature of substr
function:
string substr ( string $string , int $start [, int $length ] )
the signature of substring
function (java) looks bit different:
string substring( int beginindex, int endindex )
substring
(java) expects end-index last parameter, substr
(php) expects length.
it's not hard, get end-index in php:
$sub = substr($str, $start, $end - $start);
here working code
$start = strpos($message, '-') + 1; if ($req_type === 'rmt') { $pt_password = substr($message, $start); } else { $end = strrpos($message, '-'); $pt_password = substr($message, $start, $end - $start); }
Comments
Post a Comment