(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
mb_ereg_search — Multibyte regular expression match for predefined multibyte string
Performs a multibyte regular expression match for a predefined multibyte string.
patternThe search pattern.
optionsThe search option. See mb_regex_set_options() for explanation.
mb_ereg_search() returns true if the
multibyte string matches with the regular expression, or false
otherwise. The string for matching is set by
mb_ereg_search_init() . If
pattern is not specified, the previous one
is used.
| Version | Description |
|---|---|
| 8.0.0 |
pattern and options are nullable now.
|
Note:
The internal encoding or the character encoding specified by mb_regex_encoding() will be used as the character encoding for this function.
mb_ereg_search & subpatterns
use loop:
<?php
$str = "中国abc + abc ?!?!字符# china string";
$reg = "\w+";
mb_regex_encoding("UTF-8");
mb_ereg_search_init($str, $reg);
$r = mb_ereg_search();
if(!$r)
{
echo "null\n";
}
else
{
$r = mb_ereg_search_getregs(); //get first result
do
{
var_dump($r[0]);
$r = mb_ereg_search_regs();//get next result
}
while($r);
}
?>A 'match_all' helper function based on dulao's answer. Someone might find it useful...
<?php
function mb_ereg_match_all($pattern, $subject, &$matches, $options = '', $setOrder = false, $offset = 0) {
if (!mb_ereg_search_init($subject, $pattern, $options)) {
return false;
}
if ($offset != 0 && !mb_ereg_search_setpos($offset)) {
return false;
}
$matches = [];
if (!mb_ereg_search()) {
return 0;
}
$regs = mb_ereg_search_getregs();
$count = 0;
do {
$count++;
if ($setOrder) {
foreach ($regs as $key => $val) {
$matches[$key][] = $val;
}
} else {
$matches[] = $regs;
}
$regs = mb_ereg_search_regs();
}
while($regs);
return $count;
}
?>