Inspiration comes from Hitokoto API, Ancient Poetry API, and Daily Bible
Used a reissue from 1966 found online, this version should be safe
The source file is TXT, the format was organized using a script, currently a total of 432 entries
PHP and data files are uploaded to Github, feel free to use
- Create a file api.php in the website directory with the following content
<?php
// File to store data
$filename = 'yulu.dat';
// Specify page encoding
header('Content-type: text/html; charset=utf-8');
if(!file_exists($filename)) {
die($filename . ' data file does not exist');
}
// Read the entire data file
$data = file_get_contents($filename);
// Split into an array by line breaks
$data = explode(PHP_EOL, $data);
// Get a random line index
$result = $data[array_rand($data)];
// Remove extra line breaks (just in case)
$result = str_replace(array("\r","\n","\r\n"), '', $result);
echo 'document.write("'.htmlspecialchars($result).'");';
-
Upload the data file yulu.dat in the same directory (see above)
-
Call this code where you need to insert it
<script src="http://yourdomain/api.php"></script>
Reference: PHP Simple Implementation of Hitokoto / Random Sentence Function | Mengkun Blog