dsbruqxgt820011351
2016-03-22 14:54
浏览 785
已采纳

php搜索字符串逗号分隔并获取匹配的元素

I have a question, if anyone can help me to solve this. I have a string separated by commas, and I want to find an item that partially matches:

$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";

I need to get only the full string from partial match as a result of filter:

$result = "GenomaPrintOrder";

图片转代码服务由CSDN问答提供 功能建议

我有一个问题,如果有人可以帮我解决这个问题。 我有一个用逗号分隔的字符串,我想找到一个部分匹配的项目:

  $ search =“PrintOrder”; 
 $ string =“IDperson,Inscription,  GenomaPrintOrder,GenomaPrintView“; 
   
 
 

我需要从过滤器中获得部分匹配的完整字符串:

   $ result =“GenomaPrintOrder”; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

  • doudi2229 2016-03-22 15:06
    最佳回答

    With preg_match_all you can do like this.

    Php Code

    <?php
      $subject = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView, NewPrintOrder";
      $pattern = '/\b([^,]*PrintOrder[^,]*)\b/';
      preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
      foreach ($matches as $val) {
          echo "Matched: " . $val[1]. "
    ";
      }
    ?>
    

    Output

    Matched: GenomaPrintOrder
    Matched: NewPrintOrder
    

    Ideone Demo

    评论
    解决 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题