Last active
December 29, 2022 10:46
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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