zoukankan      html  css  js  c++  java
  • php 使用函数中遇到的坑之----strpos

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

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

    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);
    
    // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
    // 因为 'a' 是第 0 位置上的(第一个)字符。
    echo
    $pos;//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
    $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'";
    }
    ?>

    类似的函数还有这些:

    • stripos() - 查找字符串首次出现的位置(不区分大小写)
    • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置
    • strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
  • 相关阅读:
    Java课堂测试——一维数组
    05构建之法阅读笔记之二
    第八周个人总结
    团队项目第一篇——NABCD
    第七周学习进度报告
    地铁合作的第二周
    04构建之法阅读笔记之一
    第六周学习进度报告
    03人月神话阅读笔记之三
    地铁合作的第一周
  • 原文地址:https://www.cnblogs.com/joshua317/p/5825622.html
Copyright © 2011-2022 走看看