【问题标题】:indexOf and lastIndexOf in PHP?PHP中的indexOf和lastIndexOf?
【发布时间】:2015-01-11 10:43:39
【问题描述】:

在 Java 中,我们可以使用 indexOflastIndexOf。由于这些函数在 PHP 中不存在,那么这个 Java 代码的 PHP 等效项是什么?

if(req_type.equals("RMT"))
    pt_password = message.substring(message.indexOf("-")+1);
else 
    pt_password = message.substring(message.indexOf("-")+1,message.lastIndexOf("-"));

【问题讨论】:

  • 您可以通过 JavaScript 使用 IndexOf 和 LastIndexOf,因为它们存在于其中。
  • “因为这些函数在 PHP 中不存在” -- 你搜索过吗?上次我检查时,PHP 仍在提供此功能。 indexOf 被命名为 strpos()lastIndexOf 被命名为 strrpos()

标签: php indexof lastindexof


【解决方案1】:

您需要以下函数在 PHP 中执行此操作:

strpos查找子串在字符串中第一次出现的位置

strrpos查找字符串中子串最后一次出现的位置

substr返回部分字符串

这是substr 函数的签名:

string substr ( string $string , int $start [, int $length ] )

substring 函数 (Java) 的签名看起来有点不同:

string substring( int beginIndex, int endIndex )

substring (Java) 期望结束索引作为最后一个参数,但 substr (PHP) 期望长度。

不难,get the desired length by the end-index in PHP:

$sub = substr($str, $start, $end - $start);

这是工作代码

$start = strpos($message, '-') + 1;
if ($req_type === 'RMT') {
    $pt_password = substr($message, $start);
}
else {
    $end = strrpos($message, '-');
    $pt_password = substr($message, $start, $end - $start);
}

【讨论】:

    【解决方案2】:

    在php中:

    • stripos()函数用于查找不区分大小写的子字符串在字符串中第一次出现的位置。

    • strripos()函数用于查找字符串中不区分大小写的子字符串最后出现的位置。

    示例代码:

    $string = 'This is a string';
    $substring ='i';
    $firstIndex = stripos($string, $substring);
    $lastIndex = strripos($string, $substring);
    
    echo 'Fist index = ' . $firstIndex . ' ' . 'Last index = '. $lastIndex;
    

    输出:拳头索引 = 2 最后索引 = 13

    【讨论】:

      【解决方案3】:
      <?php
      // sample array
      $fruits3 = [
          "iron",
          1,
          "ascorbic",
          "potassium",
          "ascorbic",
          2,
          "2",
          "1",
      ];
      
      // Let's say we are looking for the item "ascorbic", in the above array
      
      //a PHP function matching indexOf() from JS
      echo(array_search("ascorbic", $fruits3, true)); //returns "2"
      
      // a PHP function matching lastIndexOf() from JS world
      function lastIndexOf($needle, $arr)
      {
          return array_search($needle, array_reverse($arr, true), true);
      }
      
      echo(lastIndexOf("ascorbic", $fruits3)); //returns "4"
      
      // so these (above) are the two ways to run a function similar to indexOf and lastIndexOf()
      

      【讨论】:

        【解决方案4】:

        这是最好的方法,非常简单。

        $msg = "Hello this is a string";
        $first_index_of_i = stripos($msg,'i');
        $last_index_of_i = strripos($msg, 'i');
        
        echo "First i : " . $first_index_of_i . PHP_EOL ."Last i : " . $last_index_of_i;
        

        【讨论】:

        • 请始终将您的答案放在上下文中,而不仅仅是粘贴代码。有关详细信息,请参阅here
        猜你喜欢
        • 2015-03-24
        • 2020-03-30
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        相关资源
        最近更新 更多