Log in

View Full Version : need help with php code



Marquis
02-14-2019, 04:01 PM
Hi

I use this script wich randomly returns a line of text from a txt file. The text are randomly choosed line by line. My Question : is it possible to modify this code so that the text can be over multiple lines divided by a *divider* from each other?


$quoteFile = "quotes.txt"; //File holding quotes

$fp = fopen($quoteFile, "r"); //Opens file for read
$content = fread($fp, filesize($quoteFile));
$quotes = explode("\n",$content); //Put quotes into array
fclose($fp); //Close the file

srand((double) microtime()*10000000);
$index = (rand(1, sizeof($quotes)) - 1); //Pick random quote

keyboard
02-15-2019, 07:12 AM
If you want to use something other than newlines as dividers change this line -




$quotes = explode("\n",$content); //Put quotes into array

Just replace "\n" with whatever you want to use as a divider instead. (\n is the symbol for newline)

Marquis
02-15-2019, 12:34 PM
If you want to use something other than newlines as dividers change this line -



Just replace "\n" with whatever you want to use as a divider instead. (\n is the symbol for newline)

thank you very much... just another question. same script includes img with code below. I wish to make $verzeichnis = "img/thumbs/"; relative to server root path. I tried different ways with $_SERVER['DOCUMENT_ROOT but no one works. Is it possible to do this?


<?php
$verzeichnis = "img/thumbs/";
$bildtyp = array("gif","jpg","png","bmp");
$quelle = dir($verzeichnis);
$bildname = array();
$link_target = ('_parent') ;
$linkrel = ('nofollow') ;

while($datei = $quelle->read()){$format = substr(strrchr($datei,".") ,1);
if (in_array($format,$bildtyp)){$bildname[] = $datei;}}
$quelle->close();
srand((double) microtime()*10000000);
$typ = array_rand($bildname);