Sunday, September 22, 2024
Google search engine
HomeLanguagesPHP mb_strripos() Function

PHP mb_strripos() Function

The mb_strripos() function is a PHP inbuilt case-insensitive function that is utilized to find the position for the last occurrence of a string inside another string.

Syntax:

mb_strripos($haystack, $needle, $offset = 0,encoding = null): int|false

Parameters: This function accepts four parameters described below:

  • $haystack: This parameter defines a string in which we will search our $needle parameter string.
  • $needle: This is a string parameter that searches in the $haystack string parameter.
  • $offset:  This is an optional parameter that describes where you are to start searching a string, if this defines negative it will start from the end of the $haystack.
  • $encoding: This is an optional parameter that describes the encoding of the string, if this parameter is not defined it will use an internal encoding.

Return Value: This function returns the numeric position for the last occurrence of the needle in the haystack string, otherwise it will return “false”.

Example 1: The following code describes the use of the mb_strripos() function.

PHP




<?php
    $string = 'Geeks for Geeks';
    $substring = 'for';
      
    $pos = mb_strripos($string, $substring);
      
    if ($pos !== false) {
        echo "The last occurrence of '{$substring}'
             is at position {$pos} in '{$string}'.";
    
    else {
        echo "The substring '{$substring}' 
              was not found in '{$string}'.";
    }  
?>


Output:

The last occurrence of 'for' is at position 6 in 'Geeks for Geeks'. 

Example 2: The following code describes the use of the mb_strripos() function.

PHP




<?php
    $string = 'Programming is not easy if you
               does not use neveropen';
    $substring = 'you';
    $offset = 10;
       
    $pos = mb_strripos($string, $substring, $offset);
       
    if ($pos !== false) {
        echo "The last occurrence of '{$substring}' is at 
            position {$pos} in '{$string}'
            starting from offset {$offset}.";
    }
    else {
        echo "The substring '{$substring}' was not found 
        in '{$string}' after offset {$offset}.";
    }   
?>


Output:

The last occurrence of 'you' is at position 27 in 'Programming is not easy if you 
does not use neveropen', starting from offset 10.                                  

Reference: https://www.php.net/manual/en/function.mb-strripos.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments