Created
May 14, 2020 09:32
-
-
Save riedayme/4cb9a586cbf5918659b830f0a12eefb7 to your computer and use it in GitHub Desktop.
Create Spin Text PHP
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 | |
$text = "Father fries a turkey in the cooking room accompanied by mother who makes drinks and father is really happy."; | |
$corr = [ 'father' => '{father|daddy}', 'fries' => '{fries|cooks}', 'cooking room' => '{cooking room|kitchen}' ]; | |
$words = array_keys($corr); | |
rsort($words); | |
$pattern = '~\b(?:' . implode('|', $words) . ')\b~ui'; | |
$result = preg_replace_callback($pattern, function ($m) use ($corr) { | |
$word = strtolower($m[0]); | |
return $corr[$word]; | |
}, $text); | |
echo $result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment