是否有一种优雅的方式来处理数组值,允许使用简单的字数而不是 strlen() 或使用 str_word_count -> array_count_values 等来释放自己。
例如,我只想保留包含 x 个单词的数组值。
目前我正在使用。
<?php
class Functions
{
public function processArray($array,$max,$min)
{
foreach ($array as $value)
{
/* char count */
if (strlen($value) < $max AND strlen($value) > $min)
/* word count */
if (str_word_count($value,0) < $max AND str_word_count($value,0) > $min)
{
$array2[] = $value;
}
}
return $array2;
}
}
$input = file_get_contents("files/scrape.txt");
$array = explode(".",$input);
$process = new Functions;
$output = implode(". ",$process->processArray($array,150,50));
print $output;
?>