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.
* @name Spintax
* @author Jason Davis - https://www.codedevelopr.com/
* Tutorial: https://www.codedevelopr.com/articles/php-spintax-class/
*/
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}');
@ranggacyber

This comment has been minimized.

Show comment
Hide comment
@ranggacyber

ranggacyber Sep 18, 2014

it's pretty cool .. many thanks

ranggacyber commented Sep 18, 2014

it's pretty cool .. many thanks

@salvatorecapolupo

This comment has been minimized.

Show comment
Hide comment
@salvatorecapolupo

salvatorecapolupo Jan 1, 2015

Interesting, thank you

salvatorecapolupo commented Jan 1, 2015

Interesting, thank you

@NealWalters

This comment has been minimized.

Show comment
Hide comment
@NealWalters

NealWalters Jan 28, 2015

Nice short code! Can you help explain the regex pattern: /{(((?>[^{}]+)|(?R))*)}/x
I'm trying to rewrite in Powershell/.NET. Especially, what is the ?R

NealWalters commented Jan 28, 2015

Nice short code! Can you help explain the regex pattern: /{(((?>[^{}]+)|(?R))*)}/x
I'm trying to rewrite in Powershell/.NET. Especially, what is the ?R

@lury

This comment has been minimized.

Show comment
Hide comment
@lury

lury Mar 7, 2015

@NealWalters here you go:

/
  \{                    # opening curly ({) bracket
  (                     # group $1
    (                   # group $2
      (?>               # group $3
                        # Atomic group - Matches the longest possible substring in the group and doesn't allow later backtracking to reevaluate the group.
        [^\{\}]+        # one or more characters which are not "{" or "}"
      )
      |                 # or
      (?R)              # recursive match on the pattern itself - next nesting level of {}
    )*                  # zero or more times
  )
  \}                    # ended with curly (}) bracket
/x                      # PCRE_EXTENDED - whitespaces are ignored, # means comments

lury commented Mar 7, 2015

@NealWalters here you go:

/
  \{                    # opening curly ({) bracket
  (                     # group $1
    (                   # group $2
      (?>               # group $3
                        # Atomic group - Matches the longest possible substring in the group and doesn't allow later backtracking to reevaluate the group.
        [^\{\}]+        # one or more characters which are not "{" or "}"
      )
      |                 # or
      (?R)              # recursive match on the pattern itself - next nesting level of {}
    )*                  # zero or more times
  )
  \}                    # ended with curly (}) bracket
/x                      # PCRE_EXTENDED - whitespaces are ignored, # means comments
@soulfreeza

This comment has been minimized.

Show comment
Hide comment
@soulfreeza

soulfreeza Apr 1, 2015

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 ?

soulfreeza commented Apr 1, 2015

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 ?

@erm3nda

This comment has been minimized.

Show comment
Hide comment
@erm3nda

erm3nda Jul 5, 2015

Perfectly working. Thank you so much for the explanation about the regexp.
Regexp are really cool, but not so easy to understand/explain, also has a really HUGE syntax :)

erm3nda commented Jul 5, 2015

Perfectly working. Thank you so much for the explanation about the regexp.
Regexp are really cool, but not so easy to understand/explain, also has a really HUGE syntax :)

@ghprod

This comment has been minimized.

Show comment
Hide comment
@ghprod

ghprod Sep 19, 2015

Simple but powerful :)

Thanks

ghprod commented Sep 19, 2015

Simple but powerful :)

Thanks

@cantrikndelik

This comment has been minimized.

Show comment
Hide comment
@cantrikndelik

cantrikndelik Oct 21, 2015

[ask] it is possible to spin the title also ?

cantrikndelik commented Oct 21, 2015

[ask] it is possible to spin the title also ?

@dev-masta

This comment has been minimized.

Show comment
Hide comment
@dev-masta

dev-masta Apr 21, 2016

If you want to use the [square|brackets|syntax] use this line in the process function:

'/\[(((?>[^\[\]]+)|(?R))*)\]/x',

thanks @lury

dev-masta commented Apr 21, 2016

If you want to use the [square|brackets|syntax] use this line in the process function:

'/\[(((?>[^\[\]]+)|(?R))*)\]/x',

thanks @lury

@duytanqb

This comment has been minimized.

Show comment
Hide comment
@duytanqb

duytanqb Aug 1, 2016

Thank for very helpful code.
How can I use PHP to find match text in data then replace
example my content:
$content = 'The speedy black wolf bounded over the lazy hound';
$patten = array('{the|a|}',{black|red|yellow}....);

Expect result after replace : $content = '{the|a|} speedy {black|red|yellow} wolf bounded over the lazy hound';
they will find 1 of some word in {} and if 1 of some word by | match, they will replace to the content.

duytanqb commented Aug 1, 2016

Thank for very helpful code.
How can I use PHP to find match text in data then replace
example my content:
$content = 'The speedy black wolf bounded over the lazy hound';
$patten = array('{the|a|}',{black|red|yellow}....);

Expect result after replace : $content = '{the|a|} speedy {black|red|yellow} wolf bounded over the lazy hound';
they will find 1 of some word in {} and if 1 of some word by | match, they will replace to the content.

@b3rday

This comment has been minimized.

Show comment
Hide comment
@b3rday

b3rday Aug 3, 2016

thank you for this

b3rday commented Aug 3, 2016

thank you for this

@verma-ashish

This comment has been minimized.

Show comment
Hide comment
@verma-ashish

verma-ashish Mar 10, 2017

Is there way to ignore spin for "first capital alphabet word" (e,g. names, country, etc.)?

verma-ashish commented Mar 10, 2017

Is there way to ignore spin for "first capital alphabet word" (e,g. names, country, etc.)?

@iznubadd

This comment has been minimized.

Show comment
Hide comment
@iznubadd

iznubadd Mar 20, 2017

hi
i love this scripts. is any ideas on how can it be implement with mysql data? like storing the synonyms in a database

more power

iznubadd commented Mar 20, 2017

hi
i love this scripts. is any ideas on how can it be implement with mysql data? like storing the synonyms in a database

more power

@herahadi

This comment has been minimized.

Show comment
Hide comment
@herahadi

herahadi Mar 29, 2017

hi, if i want to calculate maximum number of my spintax combination can generate unique article how to do that?

herahadi commented Mar 29, 2017

hi, if i want to calculate maximum number of my spintax combination can generate unique article how to do that?

@haknudsen

This comment has been minimized.

Show comment
Hide comment
@haknudsen

haknudsen May 19, 2017

Is there a way I could show all the possible variations?

haknudsen commented May 19, 2017

Is there a way I could show all the possible variations?

@fbparis

This comment has been minimized.

Show comment
Hide comment
@fbparis

fbparis Nov 10, 2017

The problem with this kind of spinners is that spuns do not have the same probability to be chosen.
For example, something like '{A|{B|C|D|E|F}}' will have 50% of 'A'.
I've coded another one with Python where every possible spuns have the same probability to be chosen, also you can retrieve the number of every possible spuns that can be generated from a spintax string.
You can have a look here : https://gist.github.com/fbparis/738c52e253639edb3cb41f90ea0b490c

fbparis commented Nov 10, 2017

The problem with this kind of spinners is that spuns do not have the same probability to be chosen.
For example, something like '{A|{B|C|D|E|F}}' will have 50% of 'A'.
I've coded another one with Python where every possible spuns have the same probability to be chosen, also you can retrieve the number of every possible spuns that can be generated from a spintax string.
You can have a look here : https://gist.github.com/fbparis/738c52e253639edb3cb41f90ea0b490c

@mamunur34

This comment has been minimized.

Show comment
Hide comment
@mamunur34

mamunur34 Aug 4, 2018

Very interesting. Pretty cool. As it is very slim in size and heavy in working.

mamunur34 commented Aug 4, 2018

Very interesting. Pretty cool. As it is very slim in size and heavy in working.

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