<?php
//example03.php
/**
小例子 验证变量check if the name contains only letters,
and does not contain the word name*/
$name = "Name";
try
{
try
{
//preg_match() 返回 pattern 的匹配次数。
//它的值将是0次(不匹配)或1次,因为 preg_match() 在第一次匹配后 将会停止搜索。
//preg_match_all() 不同于此,它会一直搜索 subject 直到到达结尾。
//如果发生错误 preg_match() 返回 FALSE 。
if(preg_match('/[^a-z]/i', $name))
{
throw new Exception("$name contains character other than a-z A-Z");
}
if(strpos(strtolower($name), 'name') !== false)
{
throw new Exception("$name contains the word name");
}
echo "The Name is valid";
}
catch (exception $e)
{
throw new Exception("insert name again", 0, $e);
}
}
catch (exception $e)
{
if($e->getPrevious())
{
echo "The Previous Exception is: " . $e->getPrevious()->getMessage() . "<br/>";
}
echo "The Exception is: " . $e->getMessage() . "<br/>";
}
/*
The Previous Exception is: Name contains the word name
The Exception is: insert name again
*/