MD_STD/src/MD_STD_STRINGS.php

40 lines
962 B
PHP

<?PHP
/**
* Gathers wrappers for handling strings.
*/
declare(strict_types = 1);
/**
* Encapsulates functions for handling strings.
*/
final class MD_STD_STRINGS {
/**
* Duplicates words ending in a given set of strings (e.g. dots) that serve as bind chars
* to allow indexing to then allow searching for them in either form.
*
* @param string $input Input string.
*
* @return string
*/
public static function duplicate_words_with_dots_for_indexing(string $input):string {
$charsToDuplicateOn = ',.!-';
$wordsToAdd = [];
$words = explode(' ', $input);
foreach ($words as $word) {
$trimmed = trim($word, $charsToDuplicateOn);
if ($trimmed !== $word) {
$wordsToAdd[] = $trimmed;
}
}
if (empty($wordsToAdd)) {
return $input;
}
return $input . ' ' . implode(' ', $wordsToAdd);
}
}