PHP 8.1.0 RC 4 available for testing
add a note add a note

User Contributed Notes 2 notes

up
6
dulao5 at gmail dot com
14 years ago
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);
    }
?>
up
1
Christian
2 months ago
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;
}
?>
To Top