เมื่อเราต้องการ spin บทความ หรือ ข้อความ แบบอัตโนมัติสามารถใช้ฟังก์ชั่นด้านล่างนี้ได้
<?php
$txt ="{Buy|Cheap|Best Buy}";
echo spin($txt);
function spin($s){
preg_match('#\{(.+?)\}#is',$s,$m);
if(empty($m)) return $s;
$t = $m[1];
if(strpos($t,'{')!==false){
$t = substr($t, strrpos($t,'{') + 1);
}
$parts = explode("|", $t);
$s = preg_replace("+\{".preg_quote($t)."\}+is", $parts[array_rand($parts)], $s, 1);
return spin($s);
}
?>
ไม่มีความเห็น