Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SplitWords.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $uniqueOnly;
17 
21  protected $wordsQty;
22 
27 
33  public function __construct($uniqueOnly = true, $wordsQty = 0, $wordSeparatorRegexp = '\s')
34  {
35  $this->uniqueOnly = $uniqueOnly;
36  $this->wordsQty = $wordsQty;
37  $this->wordSeparatorRegexp = $wordSeparatorRegexp;
38  }
39 
46  public function filter($str)
47  {
48  $result = [];
49  $split = preg_split('#' . $this->wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
50  foreach ($split as $word) {
51  if ($this->uniqueOnly) {
52  $result[$word] = $word;
53  } else {
54  $result[] = $word;
55  }
56  }
57  if ($this->wordsQty && count($result) > $this->wordsQty) {
58  $result = array_slice($result, 0, $this->wordsQty);
59  }
60  return $result;
61  }
62 }
__construct($uniqueOnly=true, $wordsQty=0, $wordSeparatorRegexp='\s')
Definition: SplitWords.php:33