Tutorial Read/Write Files

by in , 0

Append to a file

function fileWrite($file, $message) {
  fwrite(fopen($file, 'a'), $message . "\n");
}

Read and display entire file

function fileRead($file){
   $lines = file($file);
   foreach ($lines as $line_num => $line) {
      echo  $line,  '</br>';
   }
}

Tutorial Randomize File Name

by in , 0

function randomizeFileName( $real_file_name ) {
               $name_parts = @explode( ".", $real_file_name );
               $ext = "";
               if ( count( $name_parts ) > 0 ) {
                       $ext = $name_parts[count( $name_parts ) - 1];
               }
               return substr(md5(uniqid(rand(),1)), -16) . "." . $ext;
}

Tutorial Randomize Background Image

by in , 0

1. Above the DOCTYPE

Set up and array of filenames, which correspond to the file names of the images you are trying to randomize.

<?php
  $bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames

  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

1. CSS in the <head>

Normally you'd want to keep CSS out of the HTML, but we'll just use it here to echo out the random file name we selected above.

<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>

Tutorial Random String from Pre-Determined Characters

by in , 0

<?php
$string = "abcdwxyz456789";
for($i=0;$i<25;$i++){
   $pos = rand(0,13);
   $str .= $string{$pos};
}
echo $str;
?>

Tutorial Random Slogan Displayer

by in , 0

Create a text file called slogans.txt with permissions that it can be read by the server. Put each slogan on a different line, e.g.:

Building websites since before there was a web.
The internet is for lovers.

<?php
       $f_contents = file ("slogans.txt");
       $line = $f_contents[array_rand ($f_contents)];
       print $line;
?>

Tutorial Random Hex Color

by in , 0

Technique #1

<?php 
    
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    
?>

Then echo out the $color value anywhere you need it. For example:

<body style="background: <?php echo $color; ?>;">

Technique #2

<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>

There is also a JavaScript version.

Reference URL

Tutorial Quick Alphabetic Navigation

by in , 0

Could be useful for an address book style navigation.

<?php
foreach (range('a', 'z') as $char) {
 print '<a href="#' . $char . '">' . $char . '</a> | ';
}
?>