RemoveWords.php 742 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

namespace Drupal\views_natural_sort\Plugin\IndexRecordContentTransformation;

use Drupal\views_natural_sort\Plugin\IndexRecordContentTransformationBase as TransformationBase;

/**
 * @IndexRecordContentTransformation (
 *   id = "remove_words",
 *   label = @Translation("Remove Words")
 * )
 */
class RemoveWords extends TransformationBase {

  public function transform($string) {
    $words = $this->configuration['settings'];
    if (empty($words)) {
      return $string;
    }

    array_walk($words, 'preg_quote');
    return preg_replace(
23
      [
24 25
        '/\s(' . implode('|', $words) . ')\s+/iu',
        '/^(' . implode('|', $words) . ')\s+/iu',
26 27
      ],
      [
28 29
        ' ',
        '',
30
      ],
31 32 33 34 35
      $string
    );
  }

}