Open In App

PHP mb_ereg_search_getpos() Function

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The mb_ereg_search_getpos() function is an inbuilt function in PHP that retrieves the start position of the last match and the end position of the last match. It works with multibyte character sets.

Syntax:

mb_ereg_search_getpos(): int

Parameters: This function does not accept any parameters.

Return Value: The function returns an array containing the start and end positions of the last match, or false if there was no match. The start position is the first byte of the match and the end position is the byte immediately following the end of the match. If there are no capturing parentheses in the regular expression, then the array contains only the start position.

Program 1: The following program demonstrates the mb_ereg_search_getpos() function.

PHP




<?php
    
$string = "Geeks for Geeks";
$pattern = "o";
  
mb_regex_encoding("UTF-8");
mb_ereg_search_init($string, $pattern);
  
while (mb_ereg_search()) {
    $pos = mb_ereg_search_getpos();
    echo "The position of the last match is: " . $pos . "\n";
}    
  
?>


Output

The position of the last match is: 8

Program 2: The following program demonstrates the mb_ereg_search_getpos() function.

PHP




<?php
  
$string = "The quick brown fox jumps over the lazy dog";
$pattern = "fox";
    
mb_ereg_search_init($string, $pattern);
  
if (mb_ereg_search()) {
    $pos = mb_ereg_search_getpos();
    echo "The position of the last match is: " . $pos . "\n";
}      
  
?>


Output

The position of the last match is: 19

Reference: https://www.php.net/manual/en/function.mb-ereg-search-getpos.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads