zoukankan      html  css  js  c++  java
  • PHP之mb_substr_count使用

    mb_substr_count

    • (PHP 4 >= 4.3.0, PHP 5, PHP 7)
    • mb_substr_count — Count the number of substring occurrences
    • mb_substr_count — 统计字符串出现的次数

    Description

    int mb_substr_count ( 
        string $haystack , 
        string $needle [, 
        string $encoding = mb_internal_encoding() ] 
        )
    //Counts the number of times the needle substring occurs in the haystack string.
    //统计子字符串 needle 出现在字符串 haystack 中的次数。
    

    Parameters

    haystack

    • The string being checked.
    • 要检查的字符串。

    needle

    • The string being found.
    • 待查找的字符串。

    encoding

    • The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
    • encoding 参数为字符编码。如果省略,则使用内部字符编码。

    Return Values

    • The number of times the needle substring occurs in the haystack string.
    • 子字符串 needle 出现在字符串 haystack 中的次数。

    Examples

    <?php
    /**
     * Created by PhpStorm.
     * User: zhangrongxiang
     * Date: 2018/1/31
     * Time: 下午10:07
     */
    
    echo mb_substr_count( "This is a test", "is" ) . PHP_EOL; // 输出 2
    echo mb_substr_count( "This is a test", " " ) . PHP_EOL; // 输出 3
    echo mb_substr_count( "hello 中国! 中国 ! 中国! hello", "中国" ) . PHP_EOL; //3
    echo mb_substr_count( "hello 中国! 中国 ! 中国! hello", "中国", "gbk" ) . PHP_EOL; //3
    echo mb_substr_count( "hello 中国! 中国 ! 中国! hello", "中国", "utf-8" ) . PHP_EOL; //3
    echo mb_substr_count( "hello 中国! 中国 ! 中国! hello", "中国", "ascii" ) . PHP_EOL; //3
    
    echo "strlen : " . mb_strlen( "hello 中国! 中国 ! 中国! hello" ) . PHP_EOL; //24
    echo "mb_strlen : " . strlen( "hello 中国! 中国 ! 中国! hello" ) . PHP_EOL; //40 = 8*3 + 16
    echo substr_count( "hello 中国! 中国 ! 中国! hello", "中国" ) . PHP_EOL; //3
    echo substr_count( "hello 中国! 中国 ! 中国! hello", "中国", 2, 10 ) . PHP_EOL; //1
    
    

    文章参考

    转载注明出处

  • 相关阅读:
    查找算法:二分查找法(折半查找)
    钞票找零-贪心,动态规划算法
    PHP7与php5
    网站高并发解决方案(理论知识) 二
    loj#6566. 月之都的密码
    我的 Linux 配置
    CTSC2011 幸福路径
    WC2018 即时战略
    uoj#460 新年的拯救计划
    bzoj 5016 一个简单的询问
  • 原文地址:https://www.cnblogs.com/zhangrxiang/p/8395139.html
Copyright © 2011-2022 走看看