Current Location: Home> Function Categories> mb_ereg_search_pos

mb_ereg_search_pos

Returns the position and length of the matching portion of a multibyte regular expression of a predefined multibyte string
Name:mb_ereg_search_pos
Category:Multi-byte string
Programming Language:php
One-line Description:Search for matching substrings in multibyte strings and return their position

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:

  • pattern: The regular expression pattern to search for.
  • option: Optional parameter, option to set regular expressions. The default is 'ms', which means multi-line mode and single-byte mode. Other valid options include 'm' (multi-line mode), 'i' (case insensitive), 'x' (comments allowed), 's' (single byte mode), 'U' (non-greedy mode).

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.

Similar Functions
Popular Articles