Common form validation classes in PHP

Source: Internet
Author: User
Tags foreach arrays zip

Forms validation classes commonly used in PHP Dynamic Web page development

<?php
Class Class_post
{
Verifies whether a letter/number combination of a specified length
function Fun_text1 ($num 1, $num 2, $STR)
{
Return (Preg_match ("/^[a-za-z0-9]{". $num 1. ",". $num 2. "} $/", $str))? True:false;
}
Verify whether the specified length number
function Fun_text2 ($num 1, $num 2, $STR)
{
Return (Preg_match ("/^[0-9]{". $num 1. ",". $num 2. "} $/i ", $str))? True:false;
}
Verifies if the specified length kanji
function Fun_font ($num 1, $num 2, $STR)
{
Preg_match ("/^[\xa0-\xff]{1,4}$/", $string);
Return (Preg_match/^ ([\x81-\xfe][\x40-\xfe]) {. $num 1. ",". $num 2. "} $/", $str))? True:false;
}
Verify ID Number
function Fun_status ($STR)
{
Return (Preg_match ('/(^) ([\d]{15}|[ \d]{18}| [\d] {17}x) $)/', $str) ' True:false;
}
Verifying e-mail addresses
function Fun_email ($STR) {
Return (Preg_match ('/^[_\.0-9a-z-]+@) ([0-9a-z][0-9a-z-]+\.) +[a-z]{2,4}$/', $str))? True:false;
}
Verifying phone numbers
function Fun_phone ($STR)
{
Return (Preg_match (/^ (d{3)) | ( d{3}-))? ((0d{2,3}) |0d{2,3}-)? [1-9]d{6,7}$/, $str)] true:false;
}
Verify ZIP Code
function Fun_zip ($STR)
{
Return (Preg_match ("/^[1-9]d{5}$/", $str))? True:false;
}
Verify URL Address
function Fun_url ($STR)
{
Return (Preg_match ("/^http://[a-za-z0-9]+.[ a-za-z0-9]+[/=?%-&;_~ ' @[] ': +!] * ([^<> "]) *$/", $str))? True:false;
}
Data warehousing Escape special characters incoming values can be string or one-dimensional arrays
Function Data_join (& $data)
{
if (GET_MAGIC_QUOTES_GPC () = = False)
{
if (Is_array ($data))
{
foreach ($data as $k => $v)
{
$data [$k] = addslashes ($v);
}
}
Else
{
$data = Addslashes ($data);
}
}
return $data; Return (Preg_match (/^ (d{3)) | ( d{3}-))? ((0d{2,3}) |0d{2,3}-)? [1-9]d{6,7}$/, $str)] true:false;
}
Verify ZIP Code
function Fun_zip ($STR)
{
Return (Preg_match ("/^[1-9]d{5}$/", $str))? True:false;
}
Verify URL Address
function Fun_url ($STR)
{
Return (Preg_match ("/^http://[a-za-z0-9]+.[ a-za-z0-9]+[/=?%-&;_~ ' @[] ': +!] * ([^<> "]) *$/", $str))? True:false;
}
Data warehousing Escape special characters incoming values can be string or one-dimensional arrays
Function Data_join (& $data)
{
if (GET_MAGIC_QUOTES_GPC () = = False)
{
if (Is_array ($data))
{
foreach ($data as $k => $v)
{
$data [$k] = addslashes ($v);
}
}
Else
{
$data = Addslashes ($data);
}
}
return $data;
}
Data out of the library restore special characters incoming value can be a string or a/two-D array
Function Data_revert (& $data)
{
if (Is_array ($data))
{
foreach ($data as $k 1 => $v 1)
{
if (Is_array ($v 1))
{
foreach ($v 1 as $k 2 => $v 2)
{
$data [$k 1][$k 2] = stripslashes ($v 2);
}
}
Else
{
$data [$k 1] = stripslashes ($v 1);
}
}
}
Else
{
$data = Stripslashes ($data);
}
return $data;
}
Data display restore data format mainly for content output incoming value can be a string or a/two-D array
You should data_revert () before executing this method, and the content of the form need not be restored
Function Data_show (& $data)
{
if (Is_array ($data))
{
foreach ($data as $k 1 => $v 1)
{
if (Is_array ($v 1))
{
foreach ($v 1 as $k 2 => $v 2)
{
$data [$k 1][$k 2]=nl2br (Htmlspecialchars ($data [$k 1][$k 2]);
$data [$k 1][$k 2]=str_replace ("", "", $data [$k 1][$k 2]);
$data [$k 1][$k 2]=str_replace ("", "<br>", $data [$k 1][$k 2]);
}
}
Else
{
$data [$k 1]=nl2br (Htmlspecialchars ($data [$k 1])];
$data [$k 1]=str_replace ("", "", $data [$k 1]);
$data [$k 1]=str_replace ("", "<br>", $data [$k 1]);
}
}
}
Else
{
$data =nl2br (Htmlspecialchars ($data));
$data =str_replace ("", "", $data);
$data =str_replace ("", "<br>", $data);
}
return $data;
}
}
?> if (Is_array ($data))
{
foreach ($data as $k 1 => $v 1)
{
if (Is_array ($v 1))
{
foreach ($v 1 as $k 2 => $v 2)
{
$data [$k 1][$k 2]=nl2br (Htmlspecialchars ($data [$k 1][$k 2]);
$data [$k 1][$k 2]=str_replace ("", "", $data [$k 1][$k 2]);
$data [$k 1][$k 2]=str_replace ("", "<br>", $data [$k 1][$k 2]);
}
}
Else
{
$data [$k 1]=nl2br (Htmlspecialchars ($data [$k 1])];
$data [$k 1]=str_replace ("", "", $data [$k 1]);
$data [$k 1]=str_replace ("", "<br>", $data [$k 1]);
}
}
}
Else
{
$data =nl2br (Htmlspecialchars ($data));
$data =str_replace ("", "", $data);
$data =str_replace ("", "<br>", $data);
}
return $data;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.