Skip to content

Instantly share code, notes, and snippets.

@riedayme
Created May 14, 2020 09:32
Show Gist options
  • Save riedayme/4cb9a586cbf5918659b830f0a12eefb7 to your computer and use it in GitHub Desktop.
Save riedayme/4cb9a586cbf5918659b830f0a12eefb7 to your computer and use it in GitHub Desktop.
Create Spin Text PHP
<?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