<?php
$filename = 'data.txt';
header('Content-type: text/html; charset=utf-8');
if(!file_exists($filename)) {
die($filename . ' 数据文件不存在');
}
$data = file_get_contents($filename);
$data = explode(PHP_EOL, $data);
$result = $data[array_rand($data)];
$result = str_replace(array("\r","\n","\r\n"), '', $result);
echo $result;
?>
data.txt
里面贴入要随机显示出来的文本,一行一条 。
echo $result;
换成
echo 'document.write("'.htmlspecialchars($result).'");';
<script src="http://你的网址/api.php"></script>