PHP: Basic functions for quickstart PHP framework
Wednesday 30th April 2008

Below are a number of PHP functions which we hope you will find useful when developing your applications, all are very simple functions but each takes a minute to write, and hopefully we can save you that minute!
1. Numeric Validation
Check the variable $number is a number/integer.
Usage
print validate_number("10");
|
Or
$number = "10";
print validate_number($number);
|
2. Regular Expression and Alphanumeric Validation
Check the variable $string matches a regular expression or for example, is alphanumeric, e.g. only contains letters and numbers.
Usage (default - alphanumeric)
print validate_regex("1 String of Text");
|
Or (default - alphanumeric)
$string = "1 String of Text";
print validate_regex($string);
|
Usage (alpha)
$string = "One String of Text";
print validate_regex($string,"/[^a-zA-Z ]/");
|
Usage (alpha - lowercase only, no spaces)
$string = "onestringoftext";
print validate_regex($string,"/[^a-z]/");
|
3. Email Address Validation
Check the variable $email is a valid email address.
Usage
print validate_email("email@address.com");
|
Or
$email = "email@address.com";
print validate_email($email);
|
4. URL Validation
Check the variable $url is a valid web site URL.
Usage
print validate_url("http://www.OpenCrypt.com/");
|
Or
$url = "http://www.OpenCrypt.com/";
print validate_url($url);
|
5. URL text to linked HTML
Find text URLs in string and replaces with linked HTML URL. e.g. 'Please go to: http://www.OpenCrypt.com/' would become 'Please go to: http://www.OpenCrypt.com/'
Usage
print url_string_to_html("Please go to: http://www.OpenCrypt.com/");
|
Or
$string = "Please go to: http://www.OpenCrypt.com/";
print url_string_to_html($string);
|
6. UK Postcode Validation
United Kingdom postal code validation, fully tested post code function including validation for two postal codes which do not fit traditional formats, notably GIR0AA and TDCU1ZZ.
Usage
print validate_postcode("AA000AA");
|
Or
$postcode = "AA000AA";
print validate_postcode($postcode);
|
7. Random String Generation
Generate a random string, useful for password and session ID generation.
Usage (20 character string)
print generate_string(20);
|
Usage (10 character string, custom characters A to E, 0 to 9)
print generate_string(10,"ABCDEF0123456789");
|
8. Strip HTML Tags
Remove/strip HTML tags from string/variable, extremely useful for filtering inputs.
Usage
$text = "<html><head></head><b><u>hello world</u></b></html>";
$tags = array("html", "body", "head", "u");
print strip_selected_tags($text, $tags);
|
All Functions in One File
This text area includes all of the above functions in one ready to go PHP file, simply copy and paste and save as [something].php. This field also includes the license for the above functions which is of course Open Source.
Share this article:
|