Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: Text Spinner Class - Nested spinning supported.
<?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}');
@juanma386
Copy link

juanma386 commented Dec 27, 2018

is posible? {
Mensaje001
|Mensaje 002
{
Mensaje 002-1
|
Mensaje 002-2
}
}
To infinity data spintax.

@kamiljanicki
Copy link

kamiljanicki commented Jan 20, 2019

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!

@PhiSYS
Copy link

PhiSYS commented Jul 3, 2019

I guess we could make the asterisk lazy for a little performance boost '/{(((?>[^\{\}]+)|(?R))*?)}/x'.

/\{(((?>[^\{\}]+)|(?R))*)\}/x 816 matches, 11424 steps (~30ms)
816 matches, 11424 steps, 30ms

/\{(((?>[^\{\}]+)|(?R))*?)\}/x 816 matches, 9792 steps (~13ms)
816 matches, 9792 steps, 13ms

@EmilioNicolas
Copy link

EmilioNicolas commented Jul 3, 2019

I guess we could make the asterisk lazy for a little performance boost '/{(((?>[^{}]+)|(?R))*?)}/x'.

I forked this optimized solution and added seed rand in order to fix the spin text with srand.

@jakama
Copy link

jakama commented Aug 4, 2020

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:
{Hi|Hello}{ Mr.|} Irfaq Syed. {How are you?|How about the day?}

I get:
{Hi | Hello}
{Mr. |}
{How are you? | How about the day?}

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!

@naufalkh
Copy link

naufalkh commented Jan 14, 2021

Very interesting!
How to make the reverse?
Input: Hello to you, Mr. Smith!
Output: {Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!

@herahadi
Copy link

herahadi commented Jul 5, 2022

how to output an array of all possible combinations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment