Created
August 7, 2012 14:16
-
-
Save arcanis/3285710 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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