Saturday, September 21, 2024
Google search engine
HomeLanguagesPHP mb_ereg_search_getpos() Function

PHP mb_ereg_search_getpos() Function

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

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