back to home page

PHP Recursive Spintax Class Code

Posted on July 13th, 2011

This class is deprecated, click here to get v2.5!

The code below is a class that allows you to use spintax in PHP. Simple as pie!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class Spintax {

   function spin($string, $view=false)
   {
      $z=-1;
      $input = $this->bracketArray($string);
      for($i=0; $i<count($input);$i++){
         for($x=0; $x<count($input[$i]);$x++) {
            if(!$input[$i][$x]==""||"/n"){
               $z++;
               if(strstr($input[$i][$x], "|")){
                  $out = explode("|", $input[$i][$x]);
                  $output[$z] = $out[rand(0, count($out)-1)];
               } else {
                  $output[$z] = $input[$i][$x];
               }
            }
         }
      }
      for($i=0;$i<count($output);$i++){
         echo $output[$i];
      }
      if($view==true){
         echo "<hr>";
         $output = $this->cleanArray($output);
         $this->printArray($output);
      }
   }
   
   
   function bracketArray($str, $view=false)
   {
      $string = split("{", $str);
      for($i=0;$i<count($string);$i++){
         $_string[$i] = split("}", $string[$i]);
      }
      if($view){
         $this->printArray($_string);
      }
      return $_string;
   }
   
   function cleanArray($array){
      for($i=0;$i<count($array);$i++){
         if($array[$i]!=""){
            $cleanArray[$i] = $array[$i];
         }
      }
      return $cleanArray;
   }
   
   function printArray($array)
   {
      echo '<pre>';
      print_r($array);
      echo '</pre>';
   }
}

Below is code that shows how to use it.

1
2
3
4
<?php
include("spintax.class.php");
$spintax = new Spintax();
$spintax->spin("{{PHP|Javacript|C++|Python} is {totally|really|def} {cool|rad|fun}}"); // there is a second parameter that is a boolean, set it to true to show the array in a readable format

click here to download: spintax.class


Leave a Reply

    To syntax highlight code just use [cc lang="whatever language here"]your code here[/cc]