php – 查找正确的发生顺序

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 查找正确的发生顺序脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 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.

解决方法

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 ;
  }
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 查找正确的发生顺序全部内容,希望文章能够帮你解决php – 查找正确的发生顺序所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。