Cara Gampang Membuat Random TEXT dengan Kode PHP

11 Nov 2017 09:51 • Post By

Cara Gampang Membuat Random TEXT dengan Kode PHP


RanTex adalah skrip teks acak. RanTex akan menampilkan kutipan acak atau blok teks dari daftar pilihan Anda. Teks / kutipan dapat berisi kode HTML sehingga Anda dapat membuatnya interaktif.

Kutipan acak bisa dikecualikan baik dengan menggunakan Javascript atau yang digunakan sebagai PHP antara lain:

1. Buat file dengan nama quotes.txt lalu isikan kode dibawah ini :

First quote
Second quote
Only one quote per line please!
A quote with <b>HTML</b> code. <i>Italic anyone?</i>
You can add <a href="http://www.tutorialku.net">links</a> to quotes!
A simple quote
Just some random text...

2. Buat file dengan nama rantex.php dan isi script dibawah ini :

<?php
/* File, where the random text/quotes are stored one per line */
$settings['text_from_file'] = 'quotes.txt';

/*
   If you prefer you can list quotes that RanTex will choose from here.
   In this case set above variable to $settings['text_from_file'] = '';
*/
$settings['quotes'] = array(
'First quote',
'Multi
line
quote',
'Second quote',
'Third quote',
'Some text with <b>HTML</b> code!',
'Any single quotes \' must be escaped with a backslash',
'A quote with a <a href="http://www.phpjunkyard.com">link</a>!',
);

/*
   How to display the text?
   0 = raw mode: print the text as it is, when using RanTex as an include
   1 = Javascript mode: when using Javascript to display the quote
*/
$settings['display_type'] = 1;

/* Allow on-the-fly settings override? 0 = NO, 1 = YES */
$settings['allow_otf'] = 1;

////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT BELOW
////////////////////////////////////////////////////////////////////////////////

// Override type?
if ($settings['allow_otf'] && isset($_GET['type']))
{
	$type = intval($_GET['type']);
}
else
{
	$type = $settings['display_type'];
}

// Get a list of all text options
if ($settings['text_from_file'])
{
	$settings['quotes'] = file($settings['text_from_file']);
}

// If we have any text choose a random one, otherwise show 'No text to choose from'
if (count($settings['quotes']))
{
	$txt = $settings['quotes'][array_rand($settings['quotes'])];
}
else
{
	$txt = 'No text to choose from';
}

// Output the image according to the selected type
if ($type)
{
	// New lines will break Javascript, remove any and replace them with <br />
	$txt = nl2br(trim($txt));
	$txt = str_replace(array("\n","\r"),'',$txt);

	// Set the correct MIME type
	header("Content-type: text/javascript");

	// Print the Javascript code
	echo 'document.write(\''.addslashes($txt).'\')';
}
else
{
	echo $txt;
}
?>


3. Buat lagi file dengan nama test.php lalu isi script dibawah ini (Bertujuan untuk mencoba hasilnya bisa apa ndak)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
<title>Random text script useage example</title>
<style type="text/css">
body, td, p, input {
        color : #000000;
        font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
        font-size : 12px;
}
</style>
</head>

<body>

<p><b>Random text script usage example</b><br />
For further information on how to use the Random Text PHP script see <a href="readme.htm">Readme file</a></p>

<ol>

<li>
<p>Random quote using Javascript:<br />
<script type="text/javascript" src="rantex.php?type=1"></script></p>
</li>

<li>
<p>Random quote as a PHP include:<br />
<?php $_GET['type'] = 0; include 'rantex.php'; ?></p>
</li>


</ol>

<p>&nbsp;</p>

<p>Script by <a href="http://www.tutorialku.net" target="_blank">TUTORIALKU.NET</a>.</p>

</body>

</html>

Jika manual kamu bisa mengakses dengan kode javascript atau memanggil hasilnya pakai PHP juga bisa seperti dibawah ini.

<script type = "text / javascript" src = "rantex.php"> </ script>

<?php include 'rantex.php'; ?>

Sekian dari saya semoga bermanfaat.

Jangan lupa Subscribe ya !!

Cara Gampang Membuat Random TEXT dengan Kode PHP | Tutorialku.Net | 4.5