Take the example [TEST], which has the values of:
1. test
2. demo
3. example
and the string
Code: Select all
$string="Just a [TEST] or a [TEST]";
Code: Select all
//Just a demo or a test
My code is sorta choppy, so if anyone knows a better way please let me know.
Code: Select all
public function getData($token){
$s="SELECT * FROM `ads_tokens` WHERE `token_name` = '".$token."' ORDER BY RAND()";
$q=mysql_query($s);
while($d=mysql_fetch_assoc($q)){
$token = $d['token_value'];
}
$line = split("\n", $token);
$num = array_rand($line,1);
if($line[$num]==""){
$num = $num-1;
}
return $line[$num];
}
public function processAd($string){
preg_match_all("|\[(.*)\]|U",$string,$token,PREG_SET_ORDER);
$num = count($token)-1;
$i=0;
while($i<=$num){
$replacement[$i]=$this->getData($token[$i][1]);
$pattern[$i]="/\[".$token[$i][1]."\]/";
$final = preg_replace($pattern, $replacement, $string, 1);
$i++;
}
return $final;
}
$string="Just a [TEST] or a [TEST]"
$token=new Token;
print $token->processAd($string);