File: D:/HostingSpaces/yoda-ict/yoda-ict.nl/wwwroot/lib/general/functions.class.php
<?php
/**
* functions.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/20/13
*/
class Fn
{
public function __construct(){ }
// Converts an string so it can be used in the url
public static function encodeUrl($input)
{
// Remove whitespace
$output = trim($input);
// Lowercase
$output = strtolower($output);
// Replace & with "en" or "and"
switch(URL_LANG)
{
case 'en':
$output = str_replace('&', 'and', $output);
break;
case 'nl':
$output = str_replace('&', 'en', $output);
break;
case 'de':
$output = str_replace('&', 'und', $output);
break;
default:
$output = str_replace('&', 'and', $output);
}
// Replace special letters with normal letters
$output = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $output );
// Remove html entities
$pattern = '#(&)([a-z]*)([;$])#';
$output = preg_replace($pattern,'',$output);
// Remove all special characters
$output = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $output);
// Remove ( )
$output = str_replace('(','',$output);
$output = str_replace(')','',$output);
$output = str_replace('.','',$output);
// Replace spaces with a dash
$output = str_replace(' ','-',$output);
// Replace multiple dashes with one dash
$output = preg_replace('/-{2,}/','-',$output);
return $output;
}
/*
* Headers a 404 error
*/
public static function notFound()
{
header("HTTP/1.0 404 Not Found");
header('location:' . LANG_ROOT . '404/');
exit;
}
/*
* Redirect to a page
*/
public static function redirect($url)
{
header('location:' . $url);
exit;
}
/*
* Return a camelCase string
*/
public static function camelCase($label)
{
$label = str_replace('-',' ',$label);
$label = str_replace('_',' ',$label);
$words = explode(' ',$label);
$output = '';
foreach($words as $i => $word)
{
$word = strtolower($word);
if($i != 0) $word = ucfirst($word);
$output .= $word;
}
return $output;
}
/*
* Check if a string can be an e-mail address.
*/
public static function checkMail($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
/*
* Print
*/
public static function pre_print($array)
{
if($_SERVER['REMOTE_ADDR'] == '212.61.130.133')
{
echo '<pre>';
print_r($array);
echo '</pre>';
exit;
}
}
/*
* Checks whether input is a 2-dimensional or 1-dimensional array
* If 1-dimensional it gets converted to a 2-dimensional array
*/
public static function convert2D($array)
{
if( ! is_array($array[key($array)])) $array = array($array);
return $array;
}
}