Is valid page

Wednesday, March 17th, 2010

A little function I use to double check the items in a friendly url to see if they only use valid characters.

  1. // Check if the page name is correct
  2. function isValidPage($page){
  3.         $safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
  4.         $pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);
  5.  
  6.         // Make sure that the page name has only safe variables
  7.         foreach($pageCheck as $key => $val){
  8.                 if(!in_array($val, $safe)) return false;
  9.         }
  10.         return true;
  11. }

Display clean php code for copying

// Check if the page name is correct
function isValidPage($page){
	$safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
	$pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);

	// Make sure that the page name has only safe variables
	foreach($pageCheck as $key => $val){
		if(!in_array($val, $safe)) return false;
	}
	return true;
}