Function name: mb_ereg_search_pos()
Applicable version: PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8
Usage: The mb_ereg_search_pos() function is used to search for matching substrings in multibyte strings and return their position. This function is part of the mb_ereg_search() function and is used with the mb_ereg_search_init() and mb_ereg_search_regs() functions.
Syntax: mb_ereg_search_pos(string $pattern, string $option = 'ms')
parameter:
Return value: If a matching substring is found, a two-element array is returned, containing the start and end positions of the matching substring. If no matching substring is found, false is returned.
Example:
$text = "你好,世界!Hello, World!"; $pattern = '世界'; if (mb_ereg_search_init($text, $pattern)) { $pos = mb_ereg_search_pos(); if ($pos !== false) { echo "找到匹配的子字符串:"; echo "起始位置:" . $pos[0] . ",结束位置:" . $pos[1]; } else { echo "未找到匹配的子字符串。"; } } else { echo "正则表达式初始化失败。"; }
Output:
找到匹配的子字符串:起始位置:9,结束位置:11
In the above example, we first call the mb_ereg_search_init() function to initialize the regular expression search, and then call the mb_ereg_search_pos() function to get the position of the matching substring. Finally, judge whether a matching substring is found based on the return value and output the corresponding result.