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() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
  • 相关阅读:
    HDU.2087 剪花布条
    一个acm过来人的心得
    一个acm过来人的心得
    HDU.2190 悼念512汶川大地震遇难同胞——重建希望小学
    HDOJ.2501 Tiling_easy version
    HDOJ.2501 Tiling_easy version
    HDU
    poj3216 Prime Path(BFS)
    poj1426 Find The Multiple (DFS)
    Rikka with Nickname (简单题)
  • 原文地址:https://www.cnblogs.com/joshua317/p/5825622.html
Copyright © 2011-2022 走看看