Current Location: Home> Latest Articles> What are the common parameters of the mb_regex_set_options function? A Detailed Explanation

What are the common parameters of the mb_regex_set_options function? A Detailed Explanation

gitbox 2025-09-21

Common Parameter Explanation

mb_regex_set_options() accepts the following primary options, typically passed as strings:

  1. i - Ignore case matching
    Example: mb_regex_set_options('i');
    Effect: Makes the regular expression case-insensitive when matching letters. For example, abc can match ABC.

  2. x - Extended mode
    Example: mb_regex_set_options('x');
    Effect: Allows spaces and comments in the regular expression to improve readability. For example:

    <span><span><span class="hljs-title function_ invoke__">mb_ereg</span></span><span>(<span class="hljs-string">&#039;a # Match a
            b # Match b
            c&#039;</span>, </span><span><span class="hljs-variable">$str</span></span><span>);
    </span></span>
  3. s - Single-line mode
    Example: mb_regex_set_options('s');
    Effect: Makes the dot (.) match all characters, including newline characters. By default, . does not match newline characters.

  4. m - Multi-line mode
    Example: mb_regex_set_options('m');
    Effect: Makes ^ and $ match the beginning and end of each line, not just the start and end of the entire string.

  5. A - Force matching from the beginning of the string
    Example: mb_regex_set_options('A');
    Effect: Requires the regular expression to start matching from the beginning of the target string. If it is not at the beginning, the match fails.

  6. D - Force matching from the end of the string
    Example: mb_regex_set_options('D');
    Effect: Requires the regular expression to match from the end of the target string.


Combining Parameters

These parameters can be combined. For example, to ignore case and enable multi-line mode at the same time:

<span><span><span class="hljs-title function_ invoke__">mb_regex_set_options</span></span><span>(</span><span><span class="hljs-string">&#039;im&#039;</span></span><span>);
</span></span>

This means that the regular expression will ignore case and ^ and $ will match the start and end of each line, not just the entire string.


Usage Example

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-title function_ invoke__">mb_regex_set_options</span></span><span>(</span><span><span class="hljs-string">&#039;i&#039;</span></span><span>); </span><span><span class="hljs-comment">// Ignore case</span></span><span>
<p></span>$pattern = "php";<br>
$subject = "PHP is great!";<br>
if (mb_ereg($pattern, $subject)) {<br>
echo "Match successful!";<br>
} else {<br>
echo "Match failed!";<br>
}<br>
?><br>
</span>

Output:

<span><span>Match successful!<br></span></span>

As you can see, mb_regex_set_options('i') allows php to match PHP, achieving case-insensitive matching.