Skip to content

Instantly share code, notes, and snippets.

@arcanis
Created August 7, 2012 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arcanis/3285710 to your computer and use it in GitHub Desktop.
Save arcanis/3285710 to your computer and use it in GitHub Desktop.
<?php
class StringRandomizer {
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 );
$part = $parts[ array_rand( $parts ) ];
return $part;
}
}
$randomizer = new Randomizer( );
var_dump( $randomizer->process( "{This is my {spintax|spuntext} formatted string, my {spintax|spuntext} formatted string, my {spintax|spuntext} formatted string example.}" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment