略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: mb_ereg_search

2024-12-22

add a noteadd a note

User Contributed Notes 2 notes

up
6
dulao5 at gmail dot com
15 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
0
Christian
10 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;
}
?>

官方地址:https://www.php.net/manual/en/function.mb-ereg-search.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3