使用正则表达式测试字符串
test string using regular expression
我有一个字符串数组,想测试一下:
示例:
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
我想定义跳过的文件夹和文件数组
$skip = array('configuration.php', 'logs', 'tmp');
- logs 和 tmp 将在字符串的开头;
并从字符串数组中删除 mathched;
我怎样才能做到这一点?
谢谢!
因为您只从字符串的开头开始测试,正则表达式的解决方案会很慢。
比较两个字符串来决定保存与否。并使用 array_filter 创建新数组
$skip = array('configuration.php', 'logs/', 'tmp/');
function notSkip($str) {
global $skip;
foreach($skip as $s)
if($s == substr($str, 0, strlen($s))) return false;
return true;
}
$new = array_filter($strings, 'notSkip');
var_dump($new);
结果
array(
"install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php"
"folder/logs/log.php"
"update.php"
}
您可以使用 preg_grep
和选项 PREG_GREP_INVERT
。但是,使用简单的数组作为过滤器有点天真,因为每个项目都有条件:
$pattern = '~^(?:logs|tmp)(?:/|$)|(?:^|/)configuration\.php$~S';
$result = preg_grep($pattern, $strings, PREG_GREP_INVERT);
图案详情:
~ # pattern delimiter
^ # anchor for the start of the string
(?:logs|tmp) # non-capturing group: "logs" or "tmp"
(?:/|$) # non-capturing group: a slash or the end of the string
| # OR
(?:^|/) # non-capturing group: the start of the string or a slash
configuration\.php
$ # end of the string
~S # pattern delimiter, options*
选项S
尝试优化文字字符的交替。
这个问题的字面答案如下
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
$result = preg_grep("/^(logs|tmp|configuration.php)/", $strings, PREG_GREP_INVERT);
结果为
Array
(
[8] => folder/logs/log.php
[9] => update.php
)
但这几乎没有扩展空间。如果您想使用您的方法,将文件应用到过滤的末尾,将文件夹应用到过滤的开头,那么试试这个。
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
$result = array_filter($strings, function($input){
$skip = array('configuration.php', 'logs', 'tmp');
foreach($skip as $toSkip){
if (strpos($input, $toSkip) !== false) {
// Directory Skip - Starts with
if(strrpos($input, $toSkip, -strlen($input)) !== FALSE){
return false;
}
}
}
return true;
});
我有一个字符串数组,想测试一下:
示例:
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
我想定义跳过的文件夹和文件数组
$skip = array('configuration.php', 'logs', 'tmp');
- logs 和 tmp 将在字符串的开头;
并从字符串数组中删除 mathched; 我怎样才能做到这一点? 谢谢!
因为您只从字符串的开头开始测试,正则表达式的解决方案会很慢。
比较两个字符串来决定保存与否。并使用 array_filter 创建新数组
$skip = array('configuration.php', 'logs/', 'tmp/');
function notSkip($str) {
global $skip;
foreach($skip as $s)
if($s == substr($str, 0, strlen($s))) return false;
return true;
}
$new = array_filter($strings, 'notSkip');
var_dump($new);
结果
array(
"install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php"
"folder/logs/log.php"
"update.php"
}
您可以使用 preg_grep
和选项 PREG_GREP_INVERT
。但是,使用简单的数组作为过滤器有点天真,因为每个项目都有条件:
$pattern = '~^(?:logs|tmp)(?:/|$)|(?:^|/)configuration\.php$~S';
$result = preg_grep($pattern, $strings, PREG_GREP_INVERT);
图案详情:
~ # pattern delimiter
^ # anchor for the start of the string
(?:logs|tmp) # non-capturing group: "logs" or "tmp"
(?:/|$) # non-capturing group: a slash or the end of the string
| # OR
(?:^|/) # non-capturing group: the start of the string or a slash
configuration\.php
$ # end of the string
~S # pattern delimiter, options*
选项S
尝试优化文字字符的交替。
这个问题的字面答案如下
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
$result = preg_grep("/^(logs|tmp|configuration.php)/", $strings, PREG_GREP_INVERT);
结果为
Array
(
[8] => folder/logs/log.php
[9] => update.php
)
但这几乎没有扩展空间。如果您想使用您的方法,将文件应用到过滤的末尾,将文件夹应用到过滤的开头,那么试试这个。
$strings = array(
'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/index.html', // Match
'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php', // Match
'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
'configuration.php', // Match
'logs/error.php', // Match
'logs/index.html', // Match
'logs/joomla_update.php', // Match
'folder/logs/log.php', // Not Match
'update.php' // Not match
);
$result = array_filter($strings, function($input){
$skip = array('configuration.php', 'logs', 'tmp');
foreach($skip as $toSkip){
if (strpos($input, $toSkip) !== false) {
// Directory Skip - Starts with
if(strrpos($input, $toSkip, -strlen($input)) !== FALSE){
return false;
}
}
}
return true;
});