String validation

String validation – Validation for strings.

Introduction to String validation

This method validates string using the given format.

This method takes two arguments:

  • String.
  • An array of options.

Various option are:

  • format (mixed) - Format of the string

    • VALIDATE_NUM - Number (0-9).
    • VALIDATE_SPACE - Space (\s).
    • VALIDATE_ALPHA_LOWER - Lower case alphabets (a-z).
    • VALIDATE_ALPHA_UPPER - Upper case alphabets (A-Z).
    • VALIDATE_ALPHA - Alphabets (a-Z).
    • VALIDATE_EALPHA_LOWER - Lower case letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) and VALIDATE_ALPHA_LOWER.
    • VALIDATE_EALPHA_UPPER - Upper case letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) and VALIDATE_ALPHA_UPPER.
    • VALIDATE_EALPHA - Letters with an accent (French, ...), umlauts (German), special characters (e.g. Skandinavian) and VALIDATE_ALPHA.
    • VALIDATE_PUNCTUATION - Punctuation .,;:&"?!', "(" and ")".
    • VALIDATE_NAME - VALIDATE_EALPHA, VALIDATE_SPACE, "'" and "-".
    • VALIDATE_STREET - VALIDATE_NUM and VALIDATE_NAME, "\./", "º" and "ª".
  • min_length (int) - Minimum length.
  • max_length (int) - Maximum length.

How to use String Validation

String Validation

The following example assumes that one wants to validate a string when only uppercase alphabets, numbers and space are allowed

<?php
require_once 'Validate.php';

if (
Validate::string("1984 GEORGE ORWELL", array(
    
'format' => VALIDATE_NUM VALIDATE_SPACE VALIDATE_ALPHA_UPPER))) {
    echo 
'Valid!';
} else {
    echo 
'Invalid!';
}
?>