mb_regex_set_options() accepts the following primary options, typically passed as strings:
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.
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">'a # Match a
b # Match b
c'</span>, </span><span><span class="hljs-variable">$str</span></span><span>);
</span></span>
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.
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.
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.
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.
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">'im'</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.
<span><span><span class="hljs-meta"><?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">'i'</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.