计算字符串中每个单词的每个字符数,并使用php将少于5个字符的单词放入数组中

计算字符串中每个单词的每个字符数,并使用php将少于5个字符的单词放入数组中,php,arrays,regex,Php,Arrays,Regex,我如何才能按此顺序: - remove the period from the end of words in a string - put all the words that are less than 5 characters in an array - eliminate duplicate words 然后返回结果。例如: 我编程就像写故事一样。 $results = ('I', 'like', 'write' ); 请注意,所有单词都少于5个字符,并且只有一个“I”,因为重复项已

我如何才能按此顺序:

- remove the period from the end of words in a string
- put all the words that are less than 5 characters in an array
- eliminate duplicate words
然后返回结果。例如:

我编程就像写故事一样。

$results = ('I', 'like', 'write' );

请注意,所有单词都少于5个字符,并且只有一个“I”,因为重复项已被删除

您可以使用以下正则表达式匹配具有5个或更少字符的单词:

/\b[a-z]{1,5}\b/i
  • \b
    用于使匹配仅发生在单词边界处

用于获取已删除重复值的数组:

$text = "remove the period from the end of words in a string";
preg_match_all('/\b[a-z]{1,5}\b/i', $text, $matches);
print_r(array_unique($matches[0]));
输出:

Array
(
    [0] => the
    [1] => from
    [3] => end
    [4] => of
    [5] => words
    [6] => in
    [7] => a
)
试试这个:

$string = 'I program like I write stories.';
$string = preg_replace("/\.$/", "", $string);// remove the period from the end.
$words = explode(" " ,$string);// split string into words
foreach ($words as $wordIndex => $word) {
    if (strlen($word) > 5) { // if the length of the string is greater than 5, remove it
        unset($words[$wordIndex]);// remove the word
        }
    }
var_dump(array_unique($words));// only print the unique elements in the array
这将打印:

array (size=3)
  0 => string 'I' (length=1)
  2 => string 'like' (length=4)
  4 => string 'write' (length=5)

希望这对您有所帮助。

您可以使用此简单方法获得预期结果:

$string = 'I program like I write stories.';
$words = explode(' ', $string);
$results = [];
foreach ($words as $position => $word) {
    $word = rtrim(trim($word), '.');
    if (strlen($word) && strlen($word) <= 5 && !in_array($word, $results)) {
        $results[] = $word;
    }
}
var_dump($results);
array(3) {
  [0]=>
  string(1) "I"
  [1]=>
  string(4) "like"
  [2]=>
  string(5) "write"
}