Useful Functions

basename(path)

Given a string containing a path to a file, this function will return the base name of the file.

$base = basename($_SERVER['PHP_SELF']);

htmlentities(string)

Converts applicable characters into their html entities. I.e > changes to >

  $noHTML = htmlentities("<br />");
  print $noHTML;
  //this will print:
  &lt;br /&gt;

addslashes(string)

Escapes all single quotes, double quotes and backslashes

$str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?
echo addslashes($str);

str_replace(matches, replacements, string)

Searches and replaces all occurances of a string

// Provides: 
$bodytag = str_replace("%body%", "black", "");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy  = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

substr(string, start[, length])

Returns the portion of string specified by the start and length parameters

$rest = substr("abcdef", 1);    // returns "bcdef"
$rest = substr("abcdef", 1, 3); // returns "bcd"
$rest = substr("abcdef", 0, 4); // returns "abcd"
$rest = substr("abcdef", 0, 8); // returns "abcdef"

NEXT
INDEX
Master Index