jacek wrote:Should work actually. At the time that didn't cross my mind. I would imagine that it would be slower, but will have to test.
Hadn't thought about speed :/ And also, just afer I wrote that, I realised that I was building classes a little bit the same way, since it gives you the ability to change the code later without having to update applications, which is quite nice
Here are some classes I'm working on (gotta start the form class soon too
). Please tell me whether you think this is useful or not
Feel free to add any of this to the BetterPHP library if you find it useful or to use this code anywhere. I don't mind
[syntax=php]<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
class Validation {
const email_pattern = '#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#';
const fr_phone_pattern = '#^0[1-68]([. -]?[0-9]{2}){4}$#';
const url_pattern = '#^(http(|s)|ftp)://([0-9a-z.-_]+:[0-9a-z.-_]+@)?[0-9a-z.-]+\.[a-z]{2,4}(:[0-9]{2,6})?(/.+)?#i';
const date_pattern = '#^((0?[1-9])|([1-2][0-9])|(3[01]))[/\\.-_ ]((0?[1-9])|(1[0-2]))[/\\.-_ ]([0-9]{4})$#';
const alpha_pattern = '#^[a-z]+$#i';
const alnum_pattern = '#^[a-z0-9]+$#i';
public static function email($email) {
return (preg_match(self::email_pattern, $email)) ? TRUE : FALSE;
}
public static function frenchPhone($number) {
return (preg_match(self::fr_phone_pattern, $number)) ? TRUE : FALSE;
}
public static function url($url) {
return (preg_match(self::url_pattern, $url)) ? TRUE : FALSE;
}
public static function date($date) {
return (preg_match(self::date_pattern, $date)) ? TRUE : FALSE;
}
public static function alpha($string) {
return (preg_match(self::alpha_pattern, $string)) ? TRUE : FALSE;
}
public static function alnum($string) {
return (preg_match(self::alnum_pattern, $string)) ? TRUE : FALSE;
}
}
class MySQL {
private $conn;
private $db;
private $username;
private $password;
private $host;
private $port;
public function __construct($host, $user, $pw, $db, $port = '3306') {
$this->username = $user;
$this->password = $pw;
$this->host = $host;
$this->port = $port;
$this->db = $db;
$this->conn = mysql_connect($this->host . ':' . $this->port, $this->username, $this->password);
mysql_select_db($this->db, $this->conn);
}
}
class HtmlHelper {
/* New tag
*
* @created : 8.7.2011
* @function : open a new tag (not self closing), add classes and an ID
*/
public static function newTag($type = 'div', $classes = NULL, $id = NULL) {
$return = '<' . $type;
if(is_string($id))
$return .= ' id="' . $id . '"';
if(is_string($classes))
$return .= ' classes="' . $classes . '"';
$return .= '>';
return $return;
}
/* Close tag
*
* @created : 8.7.2011
* @function : close any non self closing tag, add structure to the HTML page by adding information about which tag is being closed
*
* $info : the id or class of the tag that is closed, for example '.myCustomClass' or '#myCustomId'
*/
public static function closeTag($type = 'div', $info = NULL) {
$return = '</' . $type;
if(is_string($info))
$return .= '<!-- /' . $info . ' -->';
$return .='>';
return $return;
}
/* Meta tag
*
* @created : 8.7.2011
* @function : add any meta tags that have a name and a content attribute
*/
public static function metaTag($name, $content) {
$return = '<meta name="' . $name . '" content="' . $content . '" />';
return $return;
}
/* Title of the HTML page
*
* @created : 8.7.2011
*/
public static function title($title) {
$return = '<title>' . $title . '</title>';
return $return;
}
/* Favicon
*
* @created : 8.7.2011
* @function : add a favicon to the page
* @filetype: PNG
*/
public static function favIcon($filePath) {
$return = '<link rel="shortcut icon" type="image/png" href="' . $filePath . '" />';
return $return;
}
/* CSS
*
* @created : 8.7.2011
* @function : add a new Cascading StyleSheet to the current page, and set the media it should apply to
* @filetype : CSS
*/
public static function css($filePath, $media = 'all') {
$return = '<link rel="stylesheet" type="text/css" href="' . $filePath . '" media="' . $media . '" />';
return $return;
}
/* JS
* @created : 8.7.2011
* @function : add a new Javascript file to the current page, and choose whether it should defer or not
* @filetype : JS
*/
public static function js($filePath, $defer = FALSE) {
$return = '<script type="text/javascript" src="' . $filePath . '"';
if($defer === TRUE)
$return .= ' defer="defer"';
$return .= '></script>';
return $return;
}
}
?>
<!DOCTYPE HTML>
<html lang="en" dir="ltr">
<head><?php
echo HtmlHelper::title('ConradK - PHP and webdesign amongst other things');
echo HtmlHelper::favIcon('favicon.png');
echo HtmlHelper::metaTag('keywords','ConradK, Conrad Kleinespel, Webdesign tutorials, PHP tutorials, Free PHP code');
echo HtmlHelper::metaTag('description','I make things. I like PHP. If I could marry someone, it would probably be a beautiful Regex
');
echo HtmlHelper::css('styles.css', 'screen, print');
echo HtmlHelper::js('custom.js',TRUE);
?></head>
<body>
<?php
?>
</body>
</html>[/syntax]