<?PHP | |
/** | |
* Spintax - A helper class to process Spintax strings. | |
* | |
* @author Jason Davis - https://www.codedevelopr.com/ | |
* | |
* Tutorial: https://www.codedevelopr.com/articles/php-spintax-class/ | |
* | |
* Updated with suggested performance improvement by @PhiSYS. | |
*/ | |
class Spintax | |
{ | |
public function process($text) | |
{ | |
return preg_replace_callback( | |
'/\{(((?>[^\{\}]+)|(?R))*?)\}/x', | |
array($this, 'replace'), | |
$text | |
); | |
} | |
public function replace($text) | |
{ | |
$text = $this->process($text[1]); | |
$parts = explode('|', $text); | |
return $parts[array_rand($parts)]; | |
} | |
} | |
/* EXAMPLE USAGE */ | |
$spintax = new Spintax(); | |
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!'; | |
echo $spintax->process($string); | |
/* NESTED SPINNING EXAMPLE */ | |
echo $spintax->process('{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {{Jason|Malina|Sara}|Williams|Davis}'); |
This comment has been minimized.
This comment has been minimized.
Interesting, thank you |
This comment has been minimized.
This comment has been minimized.
Nice short code! Can you help explain the regex pattern: /{(((?>[^{}]+)|(?R))*)}/x |
This comment has been minimized.
This comment has been minimized.
@NealWalters here you go:
|
This comment has been minimized.
This comment has been minimized.
thank you, anyway is it possible for making this spin work if only for first opening file and not working after it has been refresh ? |
This comment has been minimized.
This comment has been minimized.
Perfectly working. Thank you so much for the explanation about the regexp. |
This comment has been minimized.
This comment has been minimized.
Simple but powerful :) Thanks |
This comment has been minimized.
This comment has been minimized.
[ask] it is possible to spin the title also ? |
This comment has been minimized.
This comment has been minimized.
If you want to use the
thanks @lury |
This comment has been minimized.
This comment has been minimized.
Thank for very helpful code. Expect result after replace : $content = '{the|a|} speedy {black|red|yellow} wolf bounded over the lazy hound'; |
This comment has been minimized.
This comment has been minimized.
thank you for this |
This comment has been minimized.
This comment has been minimized.
Is there way to ignore spin for "first capital alphabet word" (e,g. names, country, etc.)? |
This comment has been minimized.
This comment has been minimized.
hi more power |
This comment has been minimized.
This comment has been minimized.
hi, if i want to calculate maximum number of my spintax combination can generate unique article how to do that? |
This comment has been minimized.
This comment has been minimized.
Is there a way I could show all the possible variations? |
This comment has been minimized.
This comment has been minimized.
The problem with this kind of spinners is that spuns do not have the same probability to be chosen. |
This comment has been minimized.
This comment has been minimized.
Very interesting. Pretty cool. As it is very slim in size and heavy in working. |
This comment has been minimized.
This comment has been minimized.
great! very useful - thanks! |
This comment has been minimized.
This comment has been minimized.
is posible? { |
This comment has been minimized.
This comment has been minimized.
Hello! Generally class working great even with 3rd or 4th nesting level. But i think there is some mysterious bug. Spintax works fine with short texts, but when text is longer than 8.000 character (whole text including synonims in brackets {}) then class cause HTTP errors (connecion reset). I make a test in separated file where was only class definition, one object and variable with text. Can somone help me with this? In php ini i have declared 2GB of ram and unlimited time of execution. Regards! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I forked this optimized solution and added seed rand in order to fix the spin text with srand. |
This comment has been minimized.
This comment has been minimized.
Hello! Great job. It is helping me a lot! I'm trying to get the parts of the spintax text separately, but I only get the ones between {} For example, in this text: I get: But not the text in the middle, that is, I don't get: Irfaq Syed. There is any Regex to get all, the spintax groups and the plain text? Thanks! |
This comment has been minimized.
This comment has been minimized.
Very interesting! |
This comment has been minimized.
it's pretty cool .. many thanks