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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:06:19  来源:igfitidea点击:

indexOf and lastIndexOf in PHP?

phpindexoflastindexof

提问by Gaurav

In Java, we can use indexOfand lastIndexOf. Since those functions don't exist in PHP, what would be the PHP equivalent of this Java code?

在 Java 中,我们可以使用indexOflastIndexOf。由于这些函数在 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 中执行此操作:

strposFind the position of the first occurrence of a substring in a string

strrposFind the position of the last occurrence of a substring in a string

substrReturn part of a string

strpos查找子字符串在字符串中第一次出现的位置

strrpos查找子字符串在字符串中最后一次出现的位置

substr返回字符串的一部分

Here's the signature of the substrfunction:

这是substr函数的签名:

string substr ( string $string , int $start [, int $length ] )

The signature of the substringfunction (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:

通过 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;