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

时间:2016-07-03 02:17:34

标签: 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”,因为删除了重复项

3 个答案:

答案 0 :(得分:2)

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

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

使用array_unique获取删除了重复值的数组:

$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
)

答案 1 :(得分:1)

试试这个:

$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)

希望这有帮助。

答案 2 :(得分:0)

您可以使用这种简单的方法来获得预期的结果:

$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"
}