[英]search string with array of values with PHP
I am trying to search a list of files and only perform work on the files that have names that contain a few values in an array. 我正在尝试搜索文件列表,并仅对名称包含数组中的几个值的文件执行工作。 I am hoping to not have to loop through the array each time I have a new filename. 我希望每次有新文件名时都不必遍历数组。
ala- 翼-
$needle = array('blah', 'bleh');
foreach($file_list as $file)
{
foreach($needle as $need)
if(strstr($file, $need) !== false)
{
// do something...
}
}
I just need to know if one of the strings in the array is in the filename, not the location of the string. 我只需要知道数组中的一个字符串是否在文件名中,而不是字符串的位置。
I would like to use something similar to strstr()
but it does not allow an array to be used as the needle. 我想使用类似于strstr()
东西,但它不允许将数组用作针。
ie- 即 -
if(strstr($haystack, array('blah', 'bleh')))
{
// do something...
}
I would prefer to stay away from regular expressions, it seems to be a sledge for a hammer's job. 我宁愿远离正规表达,似乎是锤子工作的雪橇。 any ideas? 有任何想法吗?
IMO it is fine to use regular expressions here: IMO可以在这里使用正则表达式:
$pattern = '/' . implode('|', array_map('preg_quote', $needle)) . '/i';
foreach($file_list as $file) {
if(preg_match($pattern, $file)) {
// do something
}
}
Reference: preg_quote
参考: preg_quote
Here is a more "creative" way to do it (but it uses internal loops and is very likely slower, as you actually loop several times over the array): 这是一种更“创造性”的方法(但它使用内部循环并且很可能更慢,因为您实际上在数组上循环了几次):
function cstrstr($haystack, $needle) {
return strstr($haystack, $needle) !== false;
}
foreach($file_list as $file) {
if(array_sum(array_map('cstrstr',
array_pad(array($file), count($needle), $file),
$needle))) {
// do something
}
}
But the advantages with the regex should be obvious: You have to create the pattern only once whereas in the "funny" solution you always have to create an array of length count($needle)
for every $file
. 但正则表达式的优点应该是显而易见的:你必须只创建一次模式,而在“有趣”的解决方案中,你总是需要为每个$file
创建一个长度count($needle)
数组count($needle)
。
Patrick, 帕特里克
You can use in_array(). 你可以使用in_array()。 Example: 例:
foreach($file_list as $file){
if(in_array($file, $needle)){
//--------------
// Needle found
}
}
You can find more examples here: http://php.net/manual/en/function.in-array.php 您可以在此处找到更多示例: http : //php.net/manual/en/function.in-array.php
This will perform an action on all the files that contain all the needles inside their name 这将对包含其名称中所有针的所有文件执行操作
// Loop through files
foreach($files as $file){
foreach($needles as $needle){
// if filename does not contain this needle
if(strpos($file, $needle) === false)
continue 2; // skip to next file
}
// this file matches criteria. do smth on this file
}
Do not use preg_match() if you only want to check if one string is contained in another string. 如果您只想检查另一个字符串中是否包含一个字符串,请不要使用preg_match()。 Use strpos() instead as it will be faster. 使用strpos()代替,因为它会更快。 http://php.net/manual/en/function.preg-match.php http://php.net/manual/en/function.preg-match.php
$needle = array('blah', 'bleh');
foreach ($file_list as $file) {
$result = $this->searchStrpos($file, $needle);
if ($result !== false) {
// do something...
}
}
/**
*
* @param String $string String search
* @param mixed $needle
* @param Boolean $onlyFirst return true but
* @return Boolean Return boolean for search
*/
private function searchStrpos($string, $needle, $onlyFirst = true) {
if (is_array($needle)) {
foreach ($needle as $item) {
$result = strpos($string, $item);
if ($result !== false && $onlyFirst) {
break;
}
}
} else {
$result = strpos($string, $needle);
}
return $result;
}
in_array
may work just fine, however it would require exact matches in your needle array. in_array
可以正常工作,但是它需要针阵列中的完全匹配。
You could implode
the array with some hash-string, and use the result with strstr
or strpos
— str
variant returns the first occurrence, pos
only the position of the first occurrence— Make sure to have a very unique string, with both an uncommon prefix and suffix. 你可以使用一些散列字符串来implode
数组,并使用strstr
或strpos
的结果 - str
变量返回第一个匹配项, pos
只返回第一个匹配项的位置 - 确保有一个非常独特的字符串,同时带有一个不常见的前缀和后缀。 This is rather a quick and dirty approach, but it may just work in your situation. 这是一种快速而肮脏的方法,但它可能只适用于您的情况。
EDIT I was just thinking, why don't you want to use a loop? 编辑我只是想,你为什么不想使用循环? It'll be much faster than concatenating all elements from an array. 它比连接数组中的所有元素要快得多。 Your question if there is a function to find partial matches using a reference array: it's not build-in, so you'd better use your own loop (or implement some function to do it, using a loop of course) 你的问题是否有一个使用引用数组找到部分匹配的函数:它不是内置的,所以你最好使用你自己的循环(或者实现一些函数来做,当然使用循环)
if( strstr( implode( " ", $array ), $search ) { //found }
But a function with loop and return when found is faster and less memory consuming. 但是发现循环和返回的函数更快,内存消耗更少。
function searchArrayFast( $array, $search ) { foreach ( $array as $a ) { if( strstr( $a, $search)){ return true; function searchArrayFast($ array,$ search){foreach($ array as $ a){if(strstr($ a,$ search)){return true; } } return false; 返回false; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.