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

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

Example:: String.indexOf("Blue Whale", "Blue") == 0
public indexOf ( string $string, string $search, integer $fromIndex = null ) : integer
$string string The input string
$search string The substring to search for
$fromIndex integer The index where the search should start, defaults to the beginning
Результат integer The index of the substring (>= 0) or -1 if the substring was not found
    public function indexOf($string, $search, $fromIndex = null)
    {
        $fromIndex = max(0, $fromIndex);
        if ($search === '') {
            return min(mb_strlen($string, 'UTF-8'), $fromIndex);
        }
        $index = mb_strpos($string, $search, $fromIndex, 'UTF-8');
        if ($index === false) {
            return -1;
        }
        return (int) $index;
    }

Usage Example

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