来自数组 php 的匹配模式


Match pattern from array php

我有一个数组,比如这个例子:

Array
(
    [0] => cinema
    [1] => school
    [2] => college
    [3] => social
    [4] => cinema
    [5] => School
    [6] => COllEGE
    [7] => Ccccccc
)

我只想要一次从"C"或"S"开头的整个单词,允许在单词中使用重复字符,无论它们是大写还是小写

示例输出:

cinema
college
ccccccc

array_filter与简单的过滤器(例如正则表达式或$val[0] == "c")一起使用,并array_unique

这是一个示例(未测试):

$data = array(...data...);
function check_value($val) {
  return preg_match('/^c/i', $val);
}
$output = array_unique(array_filter($data, 'check_value'));

PHP 手册的数组函数列表和字符串函数列表可能有用:

<?php
  $arr =  array ( 'cinema', 'school', 'college', 'social', 'cinema', 'School', 'COllEGE' );
  $massaged_array = massage($arr);
  $result = array_count_values($massaged_array);
  foreach ($result as $key => $value) {
    if (substr_compare($key, 'C', 0, 1) || substr_compare($key, 'S', 0, 1)){
      echo $key;
    }
  }    
  function massage ($arr) {
    $result = array();
    foreach ($arr as $value) {
      $result[] = strtolower($value);
    }
    return $result;
  }