【问题标题】:How to find the position of multiple words in a string using strpos() function?如何使用 strpos() 函数查找字符串中多个单词的位置?
【发布时间】:2015-05-22 06:25:49
【问题描述】:

我想在一个字符串中找到多个单词的位置。

例如:

$str="Learning php is fun!";

我想获得 phpfun 的位置。

我的预期输出是:-

1) Php这个词出现在第9位

2) fun这个词排在第16位。

这是我尝试过的代码,但它不适用于多个单词。

<?Php
$arr_words=array("fun","php");
$str="Learning php is fun!";
$x=strpos($str,$arr_words);
echo The word $words[1] was found on $x[1] position";
echo The word $words[2] was found on $x[2] position";

有人知道它有什么问题以及如何解决吗?

非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: php arrays string


    【解决方案1】:

    为了补充其他答案,您还可以使用正则表达式:

    $str="Learning php is fun!";
    
    if (preg_match_all('/php|fun/', $str, $matches, PREG_OFFSET_CAPTURE)) {
        foreach ($matches[0] as $match) {
            echo "The word {$match[0]} found on {$match[1]} position\n";
        }
    }
    

    另请参阅:preg_match_all

    【讨论】:

    • 这是完美的
    【解决方案2】:

    由于您无法在strpos 中加载字符串单词数组,您可以调用strpos 两次,一次用于fun,一次用于php

    $arr_words = array("fun","php");
    $str = "Learning php is fun!";
    $x[1] = strpos($str,$arr_words[0]);
    $x[2] = strpos($str,$arr_words[1]);
    echo "The word $arr_words[0] was found on $x[1] position <br/>";
    echo "The word $arr_words[1] was found on $x[2] position";
    

    Sample Output

    或者循环单词数组:

    $arr_words = array("fun","php");
    $str = "Learning php is fun!";
    foreach ($arr_words as $word) {
        if(($pos = strpos($str, $word)) !== false) {
            echo "The word {$word} was found on {$pos} position <br/>";
        }
    }
    

    Sample Output

    【讨论】:

      【解决方案3】:
      $str="Learning php is fun!";
      $data[]= explode(" ",$str);
      print_r($data);//that will show you index
      
      foreach($data as $key => $value){
       if($value==="fun") echo $key;
       if($value==="php") echo $key;
      } 
      

      键是确切的位置,但索引从 0 开始,因此请记住相应地修改您的代码,可能是 echo $key+1(多种方式,取决于您)。

      【讨论】:

        【解决方案4】:

        你做错了检查功能

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

        干草堆 要搜索的字符串。

        If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

        偏移量 如果指定,搜索将从字符串开头开始计数的字符数开始。

        参考Docs

        在你的例子中

        $arr_words=array("fun","php");
        $str="Learning php is fun!";
        $x=strpos($str,$arr_words);
        

        $arr_wordsarray 不是 string 或不是 integer

        所以你需要循环它或者需要手动传递key

        $x[1] = strpos($str,$arr_words[0]);
        $x[2] = strpos($str,$arr_words[1]);
        

        foreach($arr_words as $key => $value){
             $position = strpos($str,$value);
             echo "The word {$value} was found on {$position}th position"
        }
        

        【讨论】:

          【解决方案5】:

          只是另一个答案:

          <?php
          $arr_words=array("fun","php");
          $str="Learning php is fun!";
          
          foreach($arr_words as $needle) {
              $x = strpos($str, $needle);
              if($x)
                  echo "The word '$needle' was found on {$x}th position.<br />";
          }
          ?>
          

          【讨论】:

            【解决方案6】:

            如果第二个参数是数组,则不能使用函数 strpos。 这是最简单的方法:

            <?php
                 $words = array("php","fun");
                 $str = "Learning php is fun!";
                 foreach ($words as $word) {
                     $pos = strpos($str, $word);
                     // Found this word in that string
                     if($pos) {
                         // Show you message here
                     }
                 }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-21
              相关资源
              最近更新 更多