Home Backend Development PHP Tutorial php常用表单验证类用法实例_PHP

php常用表单验证类用法实例_PHP

May 30, 2016 am 08:44 AM
php form

本文实例讲述了php常用表单验证类用法。分享给大家供大家参考。具体如下:

<&#63;php
/**
* 页面作用:常用表单验证类
* 作 者:欣然随风
* QQ:276624915
*/
class class_post
{
//验证是否为指定长度的字母/数字组合
function fun_text1($num1,$num2,$str)
{
   Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))&#63;true:false;
}
//验证是否为指定长度数字
function fun_text2($num1,$num2,$str)
{
   return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))&#63;true:false;
}
//验证是否为指定长度汉字
function fun_font($num1,$num2,$str)
{
// preg_match("/^[\xa0-\xff]{1,4}$/", $string);
 
  return (preg_match("/^([\x81-\xfe][\x40-\xfe]){".$num1.",".$num2."}$/",$str))&#63;true:false;
}
//验证身份证号码
function fun_status($str)
{
   return (preg_match('/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/',$str))&#63;true:false;
}
//验证邮件地址
function fun_email($str){
   return (preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/',$str))&#63;true:false;
}
//验证电话号码
function fun_phone($str)
{
  return (preg_match("/^((\(\d{3}\))|(\d{3}\-))&#63;(\(0\d{2,3}\)|0\d{2,3}-)&#63;[1-9]\d{6,7}$/",$str))&#63;true:false;
}
//验证邮编
function fun_zip($str)
{
  return (preg_match("/^[1-9]\d{5}$/",$str))&#63;true:false;
}
//验证url地址
function fun_url($str)
{
  return (preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\&#63;%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",$str))&#63;true:false;
}
// 数据入库 转义 特殊字符 传入值可为字符串 或 一维数组
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;
}
// 数据出库 还原 特殊字符 传入值可为字符串 或 一/二维数组
function data_revert(&$data)
{
  if (is_array($data))
  {
  foreach ($data as $k1 => $v1)
  {
   if (is_array($v1))
   {
   foreach ($v1 as $k2 => $v2)
   {
   $data[$k1][$k2] = stripslashes($v2);
   }
   }
   else
   {
   $data[$k1] = stripslashes($v1);
   }
  }
  }
  else
  {
  $data = stripslashes($data);
  }
  Return $data;
}
// 数据显示 还原 数据格式 主要用于内容输出 传入值可为字符串 或 一/二维数组
// 执行此方法前应先data_revert(),表单内容无须此还原
function data_show(&$data)
{
  if (is_array($data))
  {
  foreach ($data as $k1 => $v1)
  {
   if (is_array($v1))
   {
   foreach ($v1 as $k2 => $v2)
   {
   $data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2]));
   $data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]);
   $data[$k1][$k2]=str_replace("\n","<br>\n",$data[$k1][$k2]);
   }
   }
   else
   {
   $data[$k1]=nl2br(htmlspecialchars($data[$k1]));
   $data[$k1]=str_replace(" "," ",$data[$k1]);
   $data[$k1]=str_replace("\n","<br>\n",$data[$k1]);
   }
  }
  }
  else
  {
  $data=nl2br(htmlspecialchars($data));
  $data=str_replace(" "," ",$data);
  $data=str_replace("\n","<br>\n",$data);
  }
  Return $data;
}
}
&#63;>

Copy after login

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles