PHP Classes

File: alter1.6.php

Recommend this page to a friend!
  Classes of Alexander Amzin   Alternate text   alter1.6.php   Download  
File: alter1.6.php
Role: Class source
Content type: text/plain
Description: Version 1.6 of class - now with regexps
Class: Alternate text
Alternative strings; (string1|string2|string3)...
Author: By
Last change: [+] Now works via regexp (rewritten).
[*] please note create_function dynamic function if you want to change pattern.
Date: 21 years ago
Size: 1,080 bytes
 

Contents

Class file image Download
<?
// Class dealing with alternative strings:
// version 1.6 - now with regexps
// String Syntax: some text (h|e|e|l|l|l|l|o| |,|world) some text
class alternate {
  var
$s;
var
$pattern;

        function
ms () {
//seeding
       
list($usec, $sec) = explode(' ', microtime());
        return (float)
$sec + ((float) $usec * 100000);
       
srand(make_seed());

            }
    function
bracket ($str) {
   
//returns bracket code
    
if (!strcmp($str,"(")) {return 1;}
     elseif (!
strcmp($str,")")) {return -1;} else {return 0;}

                }

    function
alternate ($s="") {
   
$this->ms();
   
$this->s=$s;
   
$this->pattern="/\(([^\(]*?)\)/m";
    while (
preg_match($this->pattern,$this->s)!=0)
   
$this->s=preg_replace_callback ($this->pattern,
   
create_function (
   
'$matches',
   
'$store=explode("|",$matches[1]);
    $store=explode("|",$matches[1]);
    $rand_key=$store[array_rand($store,1)];
    return $rand_key;'
),
   
$this->s);
   }
}


$alter=new alternate ("Running script\n((ha\nil|cr\ny|hello), (world|gold|silver)|how do you (do|did|man)?|anybody here?)");
echo
$alter->s;

?>