【问题标题】:Finding the correct occurrence order找到正确的发生顺序
【发布时间】:2013-09-11 14:02:43
【问题描述】:

我有一个 PHP 字符串,例如这个字符串 (haystack):

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";

现在我想按照针在字符串中出现的顺序设置一个 PHP 数组。所以这是我的针:

$needle = array(",",".","|",":");

$text 字符串中搜索针时,应该是输出:

Array (
   [0] => :
   [1] => ,
   [2] => .
   [3] => |
   [4] => :
)

这可以在 PHP 中实现吗?

这类似于question,但这是针对 JavaScript 的。

【问题讨论】:

  • 使用preg_match_allpreg_match_all('/\:|\,|\||)/i', '"这里是一个样例:这个文本,这个会被分解。这个也是|这个一个也是 :)"', $result);
  • $result 会给你你想要的输出
  • stackoverflow.com/a/18743579/1719246下方查看我的答案
  • 在下方查看我的更正答案 (stackoverflow.com/a/18743573/2304043)。它应该可以解决您的问题

标签: php


【解决方案1】:

str_split 在这里可以派上用场

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$needles = array(",",".","|",":");
$chars = str_split($string);

$found = array();

foreach($chars as $char){
  if (in_array($char, $needles)){
    $found[] = $char ;
  }
}

【讨论】:

  • 您的代码的问题是,如果任何针不止一次出现,它也会在 $found 数组中出现不止一次。您应该在“if”条件上添加“ && !in_array($char, $found)”。
  • @Scalpweb 所以,看看他的结果。这正是他所需要的。 [0][4] 索引。
  • 感谢它运行良好,而且这段代码是 Xdebug 中的禁食代码之一(并且在我的设置中工作)。
【解决方案2】:

好吧,让我们只是为了好玩

$string = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$needle = array(",",".","|",":");
$chars  = implode($needle);
$list   = array();

while (false !== $match = strpbrk($string, $chars)) {
    $list[] = $match[0];
    $string = substr($match, 1);
}

var_dump($list);

你可以see it working - 了解strpbrk

说明

strpbrk 返回第一个匹配字符之后的字符串

1 号弯

$string = "here is a sample: this text, and this will be exploded. this also | this one too :)";

// strpbrk matches ":"
$match = ": this text, and this will be exploded. this also | this one too :)";

// Then, we push the first character to the list ":"
$list = array(':');

// Then we substract the first character from the string
$string = " this text, and this will be exploded. this also | this one too :)";

2 号弯

$string = " this text, and this will be exploded. this also | this one too :)";

// strpbrk matches ","
$match = ", and this will be exploded. this also | this one too :)";

// Then, we push the first character to the list ","
$list = array(':', ',');

// Then we substract the first character from the string
$string = " and this will be exploded. this also | this one too :)";

以此类推,直到没有匹配到

5 号弯

$string = ")";

// strpbrk doesn't match and return false
$match = false;

// We get out of the while

【讨论】:

    【解决方案3】:
    $string = "here is a sample: this text, and this will be exploded. th
    is also | this one too :)";
    
    preg_match_all('/\:|\,|\||\)/i', $string, $result); 
    
    print_r(  array_shift($result) );
    

    使用preg_match_all

    模式\:|\,|\||\)

    Working demo...

    【讨论】:

      【解决方案4】:

      这将为您提供预期的结果:

           <?php
          $haystack= "here is a sample: this text, and this will be exploded. this also | this one too :)";
          $needles = array(",",".","|",":");
          $result=array();
          $len = strlen($haystack) ;
          for($i=0;$i<$len;$i++) {
              if(in_array($haystack[$i],$needles)) {
                  $result[]=$haystack[$i];
              }
          }
          var_dump($result);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-05
        • 2010-11-02
        • 1970-01-01
        • 2019-08-10
        • 2019-04-20
        • 2018-07-30
        相关资源
        最近更新 更多