Skip to content

Instantly share code, notes, and snippets.

@AucT
Last active December 29, 2022 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AucT/8aff5041d4b88636003c002d0562da97 to your computer and use it in GitHub Desktop.
Save AucT/8aff5041d4b88636003c002d0562da97 to your computer and use it in GitHub Desktop.
trim entire words (remove words at start, end and both of the string)
<?php
function ltrimWord(string $str, array $words): string
{
$words = array_map('preg_quote', $words);
return preg_replace('/(' . implode('|', $words) . ')/A', '', $str);
}
function rtrimWord(string $str, array $words): string {
$words = array_map('preg_quote', $words);
return preg_replace('/(' . implode('|', $words) . ')+$/', '', $str);
}
function trimWord(string $str, array $words): string {
$words = array_map('preg_quote', $words);
$str = preg_replace('/(' . implode('|', $words) . ')/A', '', $str);
return preg_replace('/(' . implode('|', $words) . ')+$/', '', $str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment