php php中反向strpos函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13001614/
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 04:38:24  来源:igfitidea点击:

strpos function in reverse in php

phpstringstrpos

提问by Taps101

strpos function in reverse

strpos 函数反向

I would like to find a method where i can find a character position in reverse.For example last "e" starting count in reverse.

我想找到一种方法,我可以在其中找到反向的字符位置。例如,最后一个“e”开始反向计数。

From example

从例子

$string="Kelley";
$strposition = strpos($string, 'e');

it will give me position 1.

它会给我位置 1。

回答by Manuel Schweigert

int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

Find the numeric position of the last occurrence of needle in the haystack string.

在 haystack 字符串中找到最后一次出现的 pin 的数字位置。

http://php.net/manual/en/function.strrpos.php

http://php.net/manual/en/function.strrpos.php

回答by Baba

What you need is strrposto find the position of the last occurrence of a substring in a string

您需要的是strrpos来查找字符串中子字符串最后一次出现的位置

$string = "Kelley";
$strposition = strrpos($string, 'e');
var_dump($strposition);

回答by tobspr

Try this:

尝试这个:

strrpos()

Hope that helps.

希望有帮助。

回答by Guilherme Nascimento

strriposand strrposadd $needlelength to result, eg.:

strripos并为结果strrpos添加$needle长度,例如:

<?php
$haystack = '/test/index.php';
$needle   = 'index.php';

echo strrpos($haystack, $needle);//output: 6

An alternative is use strrevfor retrieve position from the end, eg:

另一种方法是strrev用于从末尾检索位置,例如:

<?php
$haystack = 'Kelley';
$needle   = 'e';

echo strpos(strrev($haystack), strrev($needle));//Output: 1

回答by sameerNAT

Simple function , you can add:

简单的功能,你可以添加:

    function stripos_rev($hay,$ned){
    $hay_rev = strrev($hay);
    $len = strlen($hay);
    if( (stripos($hay_rev,$ned)) === false ){
        return false;
    } else {
        $pos = intval(stripos($hay_rev,$ned));
        $pos = $len - $pos;
    }
    return $pos;

}

回答by Sven

Simply as can be: strrpos()

很简单:strrpos()

This will return the first occurence of the character from the right.

这将从右侧返回字符的第一次出现。

回答by akond

function rev ($string, $char)
{
    if (false !== strrpos ($string, $char))
    {
        return strlen ($string) - strrpos ($string, $char) - 1;
    }
}

echo rev ("Kelley", "e");