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
    
    

    文章参考

    转载注明出处

  • 相关阅读:
    SDRAM(3):刷新和仲裁
    SDRAM(2):初始化
    SDRAM(1):基本介绍
    计数器(3):避免多计少计
    协议——SPI
    matlab数字图像处理 入门基础
    gVim编辑器——基本设置、常用命令、代码片段
    Pomelo的Router
    Pomelo聊天室框架
    Pomelo术语解释
  • 原文地址:https://www.cnblogs.com/zhangrxiang/p/8395139.html
Copyright © 2011-2022 走看看