Home Backend Development PHP Tutorial PHP/MySQL Three-Day Pass-Day Three (Two)_PHP Tutorial

PHP/MySQL Three-Day Pass-Day Three (Two)_PHP Tutorial

Jul 13, 2016 pm 05:20 PM
mysql php three two two and deal with us use expression Pass

三、 处理常规表达式

  我们稍微讲讲用ereg()和eregi()两个函数处理常规表达式。前面我已经提过,这些函数有的很简单,有的很复杂,看您的实际需要而定。

  使用常规表达式,您可以对一个字符串进行检查,搜索其中的一些结构模式,判定这些模式是否满足您的规定。最普遍的用法包括检查电子邮件地址是否有效(当然,即使这种办法判定有效,也不能保证邮件地址真的存在)。

  我们在这里不细究常规表达式的复杂细节了,仅仅给出几个实例。您可以使用上一页中用过的表格 - 把相应的程序代码复制过来,添加到下面的代码段中,就可以看到它是怎样工作的。

  首先,我们要确保表格中各栏只能输入字母。下面的常规表达式在用户输入一个或多个小写字母时判定为真,而输入数字是不允许的:

if (!ereg("[a-Z]", $first) || !ereg("[a-Z]", $last)) {

现在我们更进一步,检查字符串的长度是否是四到六位字符长。用[[:alpha:]]是检查字符是不是字母的简单方式。大括号内的数字检查字符个数。还要说明的是,^ 和 $ 分别代表字符串的开始和结束。

if (!ereg("^[[:alpha:]]{4,6}$", $first) || !ereg("^[[:alpha:]]{4,6}$", $last)) {

最后,我们来构造一个常规表达式,来检验电子邮件地址的有效性。这种检验方式的效果已经引发了相当多的讨论。没有什么东西是十全十美的,不过我下面给出的这段程序还是十分奏效的。

  if (!ereg(^[-!#$%&*+\./0-9=?A-Z^_`a-z{|}~]+.

@.

[-!#$%&*+\/0-9=?A-Z^_`a-z{|}~]+..

[-!#$%&*+\./0-9=?A-Z^_`a-z{|}~]+$, $last)) {

别花太多时间来细究这段代码了,还是先到下一页内容吧。

四、 简便方法

  前面的常规表达式怎么样?很有意思,是吧?要是在每个需要检查电子邮件地址的程序里都写上这么一段程序,那才真叫有意思呢?!想想看吧,得写那么乱七八糟的一段程序,还得写上那么多遍!...不过,当然了,还有更简便的方法。

  还记得前面? 学过的头文件吗?它能让我们写一段程序,象是这个电子邮件地址的检查程序,然后把这段程序包含进多个程序里面去。这样,我们要改写这段程序时,只须改动一处就行了,不用修改多个文件。

  但是,要做到这一点,我们必须用到函数。

  我们已经用过很多次函数了。每次我们查询数据库或检查字符串长度时,我们都是用函数来做的。这些函数是PHP自带的。如果您是位热心的程序员,您可以用自己编写的函数来扩充PHP本身的功能。但对本教程而言,这部分内容是太过高深了一点。我们要创建的函数不是那一种,而是写在PHP脚本程序内部的函数。

  函数就是一段程序代码,我们可以把一个或多个值传给这段代码,然后这段代码会处理我们传给它的数据并返回一个值。根据实际需要,函数可以很简单,也可以十分复杂。但是只要我们传进去一个数,然后能得到一个数,您管它里面有是复杂还是简单呢!这就是函数的可爱之处。

  PHP里的函数与C语言里的函数表现差不多。当我们定义函数时,必须指明函数需要接收什么样的数据。一开始好象不太好理解为什么它要接收数据进去,不过这样可以防止发生一些怪异的问题。函数之所以能做到这一点,是因为函数里面的变量都是私有变量,也就是说,它只在该函数内部存在。例如,您在程序中有一个变量叫$myname,如果您创建了一个函数,想让这个函数也使用那个$myname变量(值也相同),那是不行的。您可以在函数内部创建一个变量,名字也叫$myname,这两个变量可以各平相处,而各自取不同的值。不过我可不建议您这么做!您如果真的这么做了,等半年后您再来修改这样的程序时,您可能就会被弄糊涂了。

  那我们现在就来创建一个函数,先来个简单的。我们要给它取个名字,指定它要接收什么的变量。在调用这个函数之前,我们还得定义这个函数。

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;?php

function addnum($first, $second) {

$newnum = $first + $second;

return $newnum;

}

echo addnum(4,5);

?$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

这就行了!首先,我们创建了第一个自己的函数。我们定义了两个新变量,$first和$second,注意它们是怎样被定义的。在调用这个函数时,要给这两个变量按它们出现的顺序赋好值 - 4赋给$first,5赋给$second。然后我们简单地把这两个数加在一起,返回结果。“返回”在这里的意思是把结果送回去。在程序最后部分我们把数字9显示出来。

  我们再来创建一个函数,让它对我们的数据库应用有点帮助。一个能妥善处理错误的函数怎么样?试试下面的程序:

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;?php

function do_error($error) {


echo "噢,好象有点儿问题...$#@60;br$#@62;";


echo "系统报告的错误是:$error. $#@60;br$#@62;";


echo "最好是暂时关闭网站并通知系统管理员。";

die;

}


if (!$db = @mysql_connect("localhost","user", "password")) {


$db_error = "无法连接到MySQL数据库";

do_error($db_error);
}

?$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

This news has a total of 2 pages, currently on page 1 1 2

Before running the program, try closing the MySQL database, or using an incorrect username or password. You'll see friendly, helpful error messages. Careful friends will notice the @ symbol before the mysql_connect() function. It suppresses system error messages so that the program can only get relevant error messages from the do_error() function. You'll also notice that we can pass a variable defined elsewhere as a parameter to a function, rather than assigning a value directly at call time.

Remember that I said that functions use private variables, right? This is not entirely true. In fact, you can give functions access to variables outside the function. You might want to write a function that queries a database and then displays the results on multiple web pages. You don't want to pass the database connection ID to the function every time. In this case, you can define the connection ID as a global variable. For example:

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;?php

function db_query($sql) {

global $db;

$result = mysql_query($sql,$db);

return $result;

}

$sql = "SELECT * FROM mytable";

$result = db_query($sql);

?$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

This is a very simple function, but the important thing is that when you call this function, you do not have to pass the $db variable - you can make the variable accessible to the function through the word global. In this statement you can define multiple global variables, separated by commas.

Finally, you can use optional parameters to look like a real expert. The key point here is to specify a default value for the parameter when defining it in the function. Then when you call this function, if you do not specify another value for the parameter variable, the function will automatically assign the default value to this variable. If you specify other values, the default values ​​have no effect.

Don’t quite understand? For example, when you connect to a database, you almost always connect to the same server and use the same username and password. But sometimes, you also need to connect to other servers. Take a look at the program below:

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;?php



function db_connect($host = "localhost", $user="username", $pass="graeme") {

$db = mysql_connect($host, $username, $password );

return $db;

}


$old_db = db_connect();



$new_host = "site.com";

$new_db = db_connect($ne

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532609.htmlTechArticle3. Processing regular expressions Let’s talk a little bit about using the ereg() and eregi() functions to process regular expressions Mode. I have mentioned before that some of these functions are very simple and some are very complex. It depends on your...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

See all articles