PHP 中的 indexOf 和 lastIndexOf ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26923257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
indexOf and lastIndexOf in PHP?
提问by Gaurav
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?
在 Java 中,我们可以使用indexOf
和lastIndexOf
。由于这些函数在 PHP 中不存在,那么这个 Java 代码的 PHP 等价物是什么?
if(req_type.equals("RMT"))
pt_password = message.substring(message.indexOf("-")+1);
else
pt_password = message.substring(message.indexOf("-")+1,message.lastIndexOf("-"));
回答by Alfred Bez
You need the following functions to do this in PHP:
您需要以下函数在 PHP 中执行此操作:
strpos
Find the position of the first occurrence of a substring in a string
strrpos
Find the position of the last occurrence of a substring in a string
substr
Return part of a string
strpos
查找子字符串在字符串中第一次出现的位置
strrpos
查找子字符串在字符串中最后一次出现的位置
substr
返回字符串的一部分
Here's the signature of the substr
function:
这是substr
函数的签名:
string substr ( string $string , int $start [, int $length ] )
The signature of the substring
function (Java) looks a bit different:
substring
函数(Java)的签名看起来有点不同:
string substring( int beginIndex, int endIndex )
substring
(Java) expects the end-index as the last parameter, but substr
(PHP) expects a length.
substring
(Java) 需要结束索引作为最后一个参数,但substr
(PHP) 需要一个长度。
It's not hard, to get the desired length by the end-index in PHP:
$sub = substr($str, $start, $end - $start);
Here is the 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);
}
回答by Mahbub
In php:
在 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.
stripos()函数用于查找字符串中不区分大小写的子字符串第一次出现的位置。
strripos()函数用于查找不区分大小写的子字符串在字符串中最后一次出现的位置。
Sample code:
示例代码:
$string = 'This is a string';
$substring ='i';
$firstIndex = stripos($string, $substring);
$lastIndex = strripos($string, $substring);
echo 'Fist index = ' . $firstIndex . ' ' . 'Last index = '. $lastIndex;
Output: Fist index = 2 Last index = 13
输出:拳头指数 = 2 最后指数 = 13
回答by sameerNAT
<?php
// sample array
$fruits3 = [
"iron",
1,
"ascorbic",
"potassium",
"ascorbic",
2,
"2",
"1",
];
// Let's say we are looking for the item "ascorbic", in the above array
//a PHP function matching indexOf() from JS
echo(array_search("ascorbic", $fruits3, true)); //returns "2"
// a PHP function matching lastIndexOf() from JS world
function lastIndexOf($needle, $arr)
{
return array_search($needle, array_reverse($arr, true), true);
}
echo(lastIndexOf("ascorbic", $fruits3)); //returns "4"
// so these (above) are the two ways to run a function similar to indexOf and lastIndexOf()
回答by Asiamah Amos
This is the best way to do it, very simple.
这是最好的方法,非常简单。
$msg = "Hello this is a string";
$first_index_of_i = stripos($msg,'i');
$last_index_of_i = strripos($msg, 'i');
echo "First i : " . $first_index_of_i . PHP_EOL ."Last i : " . $last_index_of_i;