我有一个 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 - 找到正确的出现顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743373/