php – 查找正确的发生顺序
发布时间:2020-12-13 22:50:01 所属栏目:PHP教程 来源:网络整理
导读:我有一个 PHP字符串,例如这个字符串(haystack): $text = "here is a sample: this text,and this will be exploded. this also | this one too :)"; 现在我想按照字符串中出现针的顺序设置PHP数组.所以这是我的针: $needle = array(",",".","|",":"); 在$t
我有一个
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 ; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |