This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 
<?php defined('C5_EXECUTE') or die("Access Denied.");

/**
 * Functions useful for validating strings
 * @package Helpers
 * @category Concrete
 * @subpackage Validation
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */

class Concrete5_Helper_Validation_Strings { 

    
    /**
     * Validates an email address
     * @param string $address
     * @return bool $isvalid
     */
    public function email($em, $testMXRecord = false) {
        if (preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $em, $matches)) {
            if ($testMXRecord && function_exists('getmxrr')) {
                list($username, $domain) = explode( '@', $em );
                return getmxrr($domain, $mxrecords);
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    
    /**
     * Returns true on whether the passed field is completely alpha-numeric
     * @param string $field
     * @param bool $allow_spaces whether or not spaces are permitted in the field contents
     * @param bool $allow_dashes whether or not dashes (-) are permitted in the field contents
     * @return bool
     */
    public function alphanum($field, $allow_spaces = false, $allow_dashes = false) {
        if ($allow_spaces && $allow_dashes) {
            return !preg_match("/[^A-Za-z0-9 \-]/", $field);
        } else if ($allow_spaces) {
            return !preg_match("/[^A-Za-z0-9 ]/", $field);
        } else if ($allow_dashes) {
            return !preg_match("/[^A-Za-z0-9\-]/", $field);
        } else {
            return !preg_match('/[^A-Za-z0-9]/', $field);
        }
    }
    
    /** 
     * Returns true if the passed field is a valid "handle" (e.g. only letters, numbers, or a _ symbol
     */
    public function handle($handle) {
        return !preg_match("/[^A-Za-z0-9\_]/", $handle);
    }

    
    /**
     * Returns false if the string is empty (including trim())
     * @param string $field
     * @return bool
     */
    public function notempty($field) {
        return ((is_array($field) && count($field) > 0) || (is_string($field) && trim($field) != ''));
    }   
    
    /** 
     * Returns true on whether the passed string is larger or equal to the passed length
     * @param string $str
     * @param int $length
     * @return bool
     */
    public function min($str, $length) {
        return strlen(trim($str)) >= $length;
    }
    
    /** 
     * Returns true on whether the passed is smaller or equal to the passed length
     * @param string $str
     * @param int $length
     * @return bool
     */
    public function max($str, $length) {
        return strlen(trim($str)) <= $length;
    }
    
    /**
     * Returns 0 if there are no numbers in the string, or returns the number of numbers in the string
     * @param string $str
     * @return int
     */
    public function containsNumber($str) {
        return strlen(trim(preg_replace('/([^0-9]*)/', '', $str)));
    }
    
    /**
     * Returns 0 if there are no upper case letters in the string, or returns the number of upper case letters in the string
     * @param string $str
     * @return int
     */
    public function containsUpperCase($str) {
        return strlen(trim(preg_replace('/([^A-Z]*)/', '', $str)));
    }

    /**
     * Returns 0 if there are no lower case letters in the string, or returns the number of lower case letters in the string
     * @param string $str
     * @return int
     */
    public function containsLowerCase($str) {
        return strlen(trim(preg_replace('/([^a-z]*)/', '', $str)));
    }

    /**
     * Returns 0 if there are no symbols in the string, or returns the number of symbols in the string
     * @param string $str
     * @return int
     */ 
    public function containsSymbol($str) {
        return strlen(trim(preg_replace('/([a-zA-Z0-9]*)/', '', $str))); //we replace a-z and numbers and see if there is anything left.
    }
    
    /**
     * Returns true if the string contains another string
     * @param string $str
     * @param array $cont
     * @return bool
     */
    public function containsString($str, $cont = array()) {
        $arr = (!is_array($cont)) ? array($cont) : $cont; // turn the string into an array
        foreach ($arr as $item) {
            if (strstr($str, $item) !== false) {
                return true;
            }
        }
        return false;
    }
}