zoukankan      html  css  js  c++  java
  • strpos、 strstr、 substr三个函数的对比讲解

    mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
    返回 needle 在 haystack 中首次出现的数字位置。
    同时注意字符串位置是从0开始,而不是从1开始的(没有提供offset时)。
    offset

    如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计

     
    如果没找到 needle,将返回 FALSE(因此应该用===来测试返回的值)
    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);

    // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
    // 因为 'a' 是第 0 位置上的(第一个)字符。
    if ($pos === false) {
        echo "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    ?>
     
    <?php
    // 忽视位置偏移量之前的字符进行查找
    $newstring = 'abcdef abcdef';
    $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
    ?>
     
    • stripos() - 查找字符串首次出现的位置(不区分大小写)
    • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置
    • strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
     
     
    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  函数strchr是他的别名

    返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。

    before_needle

    若为 TRUEstrstr() 将返回 needle 在 haystack 中的位置之前的部分。

    Note:

    该函数区分大小写。如果想要不区分大小写,请使用 stristr()

    Note:

    如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos() 函数。
     
    <?php
    $email  = 'name@example.com';
    $domain = strstr($email, '@');
    echo $domain; // 打印 @example.com

    $user = strstr($email, '@', true); // 从 PHP 5.3.0 起
    echo $user; // 打印 name
    ?>
     
    • stristr() - strstr 函数的忽略大小写版本
     
    string substr ( string $string , int $start [, int $length ] )

    返回字符串 string 由 start 和 length 参数指定的子字符串。

    string

    输入字符串。

    start

    如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

    如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。

    如果 string 的长度小于或等于 start,将返回 FALSE
    $rest = substr("abcdef", -1);    // 返回 "f"
    $rest = substr("abcdef", 0, -1);  // 返回 "abcde"
    $rest = substr("abcdef", 2, -1);  // 返回 "cde"
    $rest = substr("abcdef", 4, -4);  // 返回 ""
    $rest = substr("abcdef", -3, -1); // 返回 "de"
    echo substr('abcdef', 1);     // bcdef
    echo substr('abcdef', 1, 3);  // bcd
    echo substr('abcdef', 0, 4);  // abcd
    echo substr('abcdef', 0, 8);  // abcdef
    echo substr('abcdef', -1, 1); // f

    // 访问字符串中的单个字符
    // 也可以使用中括号
    $string = 'abcdef';
    echo $string[0];                 // a
    echo $string[3];                 // d
    echo $string[strlen($string)-1]; // f
  • 相关阅读:
    ubuntu怎么安装下载工具uget+aria2 for firefox
    #pragma once
    opencv3在CMakeLists.txt中的调用问题
    opencv之Mat数据类型
    windows10下笔记本电脑外接显示器设置
    ubuntu16.04下笔记本电脑扩展双屏安装过程
    【问题收录】Ubuntu14.04连接两个双显示器失败的解决方案
    获取jsapi_ticket
    微信公众号中高德地图显示路线开发
    微信公众号中高德地图显示路线
  • 原文地址:https://www.cnblogs.com/ryanlamp/p/6432495.html
Copyright © 2011-2022 走看看