Neos\Eel\Helper\StringHelper::lastIndexOf PHP Метод

lastIndexOf() публичный Метод

Example:: String.lastIndexOf("Developers Developers Developers!", "Developers") == 22
public lastIndexOf ( string $string, string $search, integer $toIndex = null ) : integer
$string string The input string
$search string The substring to search for
$toIndex integer The position where the backwards search should start, defaults to the end
Результат integer The last index of the substring (>=0) or -1 if the substring was not found
    public function lastIndexOf($string, $search, $toIndex = null)
    {
        $length = mb_strlen($string, 'UTF-8');
        if ($toIndex === null) {
            $toIndex = $length;
        }
        $toIndex = max(0, $toIndex);
        if ($search === '') {
            return min($length, $toIndex);
        }
        $string = mb_substr($string, 0, $toIndex, 'UTF-8');
        $index = mb_strrpos($string, $search, 0, 'UTF-8');
        if ($index === false) {
            return -1;
        }
        return (int) $index;
    }

Usage Example

 /**
  * @test
  * @dataProvider lastIndexOfExamples
  */
 public function lastIndexOfWorks($string, $search, $fromIndex, $expected)
 {
     $helper = new StringHelper();
     $result = $helper->lastIndexOf($string, $search, $fromIndex);
     $this->assertSame($expected, $result);
 }