indexOf and lastIndexOf in PHP?

后端 未结 4 2079
孤独总比滥情好
孤独总比滥情好 2021-02-05 01:38

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

<         


        
4条回答
  •  情书的邮戳
    2021-02-05 01:46

    In 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.

    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

提交回复
热议问题