Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在最后一个字符之后和另一个字符之前进行分析_Php_Regex_Parsing_Substring - Fatal编程技术网

Php 在最后一个字符之后和另一个字符之前进行分析

Php 在最后一个字符之后和另一个字符之前进行分析,php,regex,parsing,substring,Php,Regex,Parsing,Substring,我有一个数组,看起来是这样的: array(7) [ 0 => "account_bad_rooms_view-1" (24) 1 => "account_view-2" (14) 2 => "admin_collector_view-5" (22) 3 => "admin_collector_edit-5" (22) 4 => "machine_service_item_edit-57" (28) 5 => "money_view-5

我有一个数组,看起来是这样的:

array(7) [
  0 => "account_bad_rooms_view-1" (24)
  1 => "account_view-2" (14)
  2 => "admin_collector_view-5" (22)
  3 => "admin_collector_edit-5" (22)
  4 => "machine_service_item_edit-57" (28)
  5 => "money_view-58" (13)
  6 => "mu_edit-61" (10)
]
我需要得到的子字符串将只包含
“edit”
“view”
,所以我需要在最后一个下划线之后和破折号之前解析这个字符串。到目前为止,我有:

foreach ($permissionsArray as $selectId => $permissions) {

    preg_match('~_(.*?)-~', $permissions, $output);
    dump($output[1]);

}
这适用于只有一个下划线的字符串,因此结果是:

"bad_rooms_view"
"view"
"collector_view"
"collector_edit"
"service_item_edit"
"view"
"edit"
我怎样才能用简单的方法来做呢

_([^_]*)-

您可以改为使用此函数。这将不允许另一个

您可以使用函数获得输出,而不使用任何循环:

$array=array("account_bad_rooms_view-1", "account_view-2", "admin_collector_view-5",
  "admin_collector_edit-5", "machine_service_item_edit-57", "money_view-58", "mu_edit-61");

$matches = preg_filter('/^.+?_([^-_]+)-.*$/', '$1', $array);

print_r($matches);
输出:

Array
(
    [0] => view
    [1] => view
    [2] => view
    [3] => edit
    [4] => edit
    [5] => view
    [6] => edit
)

我也探讨过这一点,但是那些与模式不匹配的条目呢?它们将被跳过,我认为它们应该被保留。我想出了一个好主意。