【问题标题】:Is there any function similar to stristr() in PHP 5.2PHP 5.2 中有没有类似 stristr() 的函数
【发布时间】:2011-12-13 09:20:47
【问题描述】:

有没有类似stristr()的功能?我想用stristr(),但是我的PHP版本是5.2.9我不能用。

所以我需要一个提供相同功能的类似函数。

<?php
$email = 'USER@EXAMPLE.com';
echo stristr($email, 'e'); // outputs ER@EXAMPLE.com
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US
?>

我该怎么做?

【问题讨论】:

    标签: php php-5.2


    【解决方案1】:

    使用stripossubstr

    echo substr($email, 0, stripos($email, 'e'));
    

    【讨论】:

      【解决方案2】:

      如果你想使用'before needle'功能,使用2参数版本实现自己是微不足道的......

       function stristr_bn($haystack, $needle)
       {
          $post=stristr($haystack, $needle);
          if ($post===false) return false;
          return substr($haystack, 0, strlen($haystack)-strlen($post)-strlen($needle));
       }
      

      然而,对于解析 ADRR_SPEC 的问题,这是一个非常混乱的解决方案(无论实现如何)。

      【讨论】:

        猜你喜欢
        • 2019-06-04
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 2011-08-01
        相关资源
        最近更新 更多