• php strpos返回字符串首次出现的位置


    (PHP 4, PHP 5, PHP 7)

    strpos — 查找字符串首次出现的位置

    说明

    mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

    返回 needlehaystack 中首次出现的数字位置。

    参数

     

    haystack

    在该字符串中进行查找。

    needle

    如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。

    offset

    如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计。和 strrpos()strripos()不一样,这个偏移量不能是负数。

    返回值

    返回 needle 存在于 haystack 字符串起始的位置(独立于 offset)。同时注意字符串位置是从0开始,而不是从1开始的。

    如果没找到 needle,将返回 FALSE

    Warning

    此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

    范例

     

    Example #1 使用 ===

    <?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";
    }
    ?>

    Example #2 使用 !==

    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);

    // 使用 !== 操作符。使用 != 不能像我们期待的那样工作,
    // 因为 'a' 的位置是 0。语句 (0 != false) 的结果是 false。
    if ($pos !== false) {
         echo "The string '$findme' was found in the string '$mystring'";
             echo " and exists at position $pos";
    } else {
         echo "The string '$findme' was not found in the string '$mystring'";
    }
    ?>

    Example #3 使用位置偏移量

    <?php
    // 忽视位置偏移量之前的字符进行查找
    $newstring = 'abcdef abcdef';
    $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
    ?>

    注释

    Note: 此函数可安全用于二进制对象。

    参见

     

    • stripos() - 查找字符串首次出现的位置(不区分大小写)
    • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置
    • strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
    • strstr() - 查找字符串的首次出现
    • strpbrk() - 在字符串中查找一组字符的任何一个字符
    • substr() - 返回字符串的子串
    • preg_match() - 执行一个正则表达式匹配
  • 相关阅读:
    三、Antd react 组件调用ref的用法获取实例
    三、gitextension 报错无法检出版本库的时候
    二、安装引入 antd
    一、React项目骨架搭建
    一、JAVA基础知识
    五、Maven创建Spring(IDEA2019-3-3)
    python爬取快手ios端首页热门视频
    接口测试之基础篇--http协议
    一些测试面试题
    性能测试一些相关的概念
  • 原文地址:https://www.cnblogs.com/jasonxu19900827/p/5264501.html
Copyright © 2020-2023  润新知