web-dev-qa-db-ja.com

PHPの逆のstrpos関数

strpos関数の逆

文字位置を逆に見つけることができる方法を見つけたいと思います。たとえば、最後の「e」は逆にカウントを開始します。

例から

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

ポジション1になります。

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

Haystack文字列で最後に見つかったneedleの数値位置を見つけます。

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

27

必要なのは strrpos で、文字列内で最後に出現する部分文字列の位置を検索します

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

これを試して:

strrpos()

お役に立てば幸いです。

3
tobspr

strriposおよびstrrpos add $needle結果までの長さ。例:

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

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

別の方法として、末尾から位置を取得するには strrev を使用します。例:

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

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

単純にできること:strrpos()

これにより、右から最初に出現する文字が返されます。

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

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

シンプルな機能、あなたが追加することができます:

    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;

}
1
sameerNAT