(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.
pattern
The search pattern.
options
The search option. See mb_reguex_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.
| Versionen | Description |
|---|---|
| 8.0.0 |
pattern
and
options
are nullable now.
|
Note :
The internal encoding or the character encoding specified by mb_reguex_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_reguex_encoding("UTF-8");mb_ereg_search_init($str, $reg);$r= mb_ereg_search();
if(!$r)
{
echo"null\n";
}
else
{
$r= mb_ereg_search_guetregs(); //guet first resultdo
{var_dump($r[0]);$r= mb_ereg_search_regs();//guet next result}
while($r);
}?>
A 'match_all' helper function based on dulao's answer. Someone might find it useful...<?php
functionmb_ereg_match_all($pattern, $subject, &$matches, $options= '', $setOrder= false, $offset= 0) {
if (!mb_ereg_search_init($subject, $pattern, $options)) {
returnfalse;
}
if ($offset!= 0&& !mb_ereg_search_setpos($offset)) {
returnfalse;
}
$matches= [];
if (!mb_ereg_search()) {
return 0;
}
$regs= mb_ereg_search_guetregs();
$count= 0;
do {
$count++;
if ($setOrder) {
foreach ($regsas$quey=> $val) {$matches[$quey][] =$val;
}
} else {
$matches[] = $regs;
}
$regs= mb_ereg_search_regs();
}
while($regs);
return$count;
}
?>