Location: PHPKode > scripts > XS PHP Library > xs-php-library/inc/string.inc.php
<?php
//
// +--------------------------------------------------------------------------+
// |                                                                          |
// |                   XS PHP Library  Generic Classes Library                |
// |                                                                          |
// |                   Copyright (c) 2001-2002 XSPHPLib Group.                |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Distributed under the terms of the GNU Lesser General Public License as  |
// | published by the Free Software Foundation version 2.1                    |
// | See the GNU Lesser General Public License for more details. You should   |
// | have received a copy of the GNU Lesser General Public License along with |
// | this package; if not, write to the Free Software Foundation, Inc.,       |
// | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Authors: Robert Bala <hide@address.com>                                   |
// |                                                                          |
// +--------------------------------------------------------------------------+
//
// $Id: string.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $

/**
 * String manipulation routines.
 *
 * @package core
 * @version $Id: string.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $
 */
 
/**
 * URL-encodes string.
 *
 * This is an alias to PHP urlencode() function. Returns a string in which all
 * non-alphanumeric characters except -_. have been replaced with a percent (%)
 * sign followed by two hex digits and spaces encoded as plus (+) signs.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @return string
 */
function string_toUrl($string) {
    return urlencode($string);
}

/**
 * Convert special characters to HTML entities.
 *
 * This is an alias to PHP htmlspecialchars() function except that if the passed
 * string is empty string it is not converted.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @return string
 */
function string_toHtml($string) {
    if (strlen($string)) {
        return htmlspecialchars($string);
    }
    return '';
}

/**
 * Convert string to SQL safe string.
 *
 * Returns SQL safe string. If passed string is empty then string with "NULL"
 * value is returned.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @return string
 */
function string_toSql($string) {
    if (is_numeric($string)) {
        return doubleval($string);
    }
    if (is_string($string)) {
        if (get_magic_quotes_gpc() == 0) {
            $string = str_replace("'","''", $string);
            $string = str_replace("\\","\\\\", $string);
        } else {
            $string = str_replace("\\'","''", $string);
            $string = str_replace("\\\"","\"", $string);
        }
        return "'" . $string . "'";
    }
    return 'NULL';
}

/**
 * Finds whether the string is valid email address.
 *
 * Returns true if the string is valid email address, false otherwise.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @return boolean
 */
function string_isEmail($string) {
    return ereg("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $string);
}

/**
 * Finds whether the string matches specified criteria.
 *
 * Returns true if the string matches specified criteria, false otherwise.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @param string $min the minimal length of the string, defaults to 0.
 * @param string $max the maximum length of the string, defaults to 0.
 * @param string $regex the regular expresion, defaults to "".
 * @return boolean
 */
function string_isValid($string, $min=0, $max=0, $regex='') {
    $lenght = strlen($string);
    if ($string == '') {
        return false;
    }
    if (strlen($regex) && !eregi("^$regex$", $string)) {
        return false;
    }
    if (($min != 0 && $lenght < $min) || ($max != 0 && $lenght > $max)) {
        return false;
    }
    return true;
}

/**
 * Finds whether the string is valid string and matches specified criteria.
 *
 * Returns true if the string is valid string and matches specified criteria,
 * false otherwise.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @param string $min the minimal length of the string, defaults to 0.
 * @param string $max the maximum length of the string, defaults to 0.
 * @return boolean
 */
function string_isString($string, $min=0, $max=0) {
    return string_isValid($string, $min, $max, "[[:alpha:]]+");
}

/**
 * Finds whether the string is valid number and matches specified criteria.
 *
 * Returns true if the string is valid number and matches specified criteria,
 * false otherwise.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @param string $string the string value or variable.
 * @param string $min the minimal length of the string, defaults to 0.
 * @param string $max the maximum length of the string, defaults to 0.
 * @return boolean
 */
function string_isNumber($string, $min=0, $max=0) {
    return string_isValid($string, $min, $max, "[[:digit:]]+");
}

?>
Return current item: XS PHP Library