PHP-Cookbook-chapter5

程式

//'5.1'指定樣式替換的結果
$session_name = $session_txt['5.1'];
echo $session_name."<br>";


	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   PHP的常規表示式並不影響到他們所處理的字串內容,而是根據所給的<br>
   樣式和字串傳回一新的字串,因此需要一個新的變數來接它。
   
<br>
</fieldset>
";
echo $message."<br>";
//'5.9'檢驗網頁傳送的資料
$session_name = $session_txt['5.9'];
echo $session_name."<br>";

@exec (escapeshellcmd ($input),$output);

if (!@empty ($name)){
	die ("You have to supply Your name");
}
	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   exec (esecapeshellcmd (\$input),\$output)會跳脫所有的<br>
   shell萬用字元,讓使用者輸入的內容無法危害你的系統安全。<br>
   empty()函式只檢查有沒有值,不會檢查是否為有效值。
   
<br>
</fieldset>
";
echo $message."<br>";
//'5.12'檢查重複的字
$session_name = $session_txt['5.12'];
echo $session_name."<br>";

$seen = array();
$paragraph ="The ugly lady chased the handsome man";
$paragraph =preg_split("/\s+/",$paragraph);

foreach ($paragraph as $word){
	@$seen [strtolower ($word)]++;
}
$seen = show_array($seen);
print "There were $seen [the] occurrences of the word 'the' <br>";

function get_dupes ($str){
	$str = strtolower ($str);
	$words = preg_split ("/\s+/",$str);
	$seen = array();
	$start_pos = array();
	$i = 0;
	
	foreach ($words as $word){ //取字串的迴圈
	    //二維的陣列,包含單字的位置
		@$seen [$word][$i] = strpos ($str,$word,$start_pos [$word]);
		//指定起始位置以避免重複
		$start_pos [$word] = $seen [$word][$i] +strlen ($word);
		$i++;
	}
	return ($seen);
}
$str = " The The the Hello Truck Hello The the Jester Rye";
echo $str."<br>";
$duplicates = get_dupes ($str);
var_dump ($duplicates); //印出陣列的相關訊息

	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    var_dump()印出陣列的相關訊息<br>
	strtolower()將字串轉成小寫<br>
    strpos()找出字串首次出現的位置<br>
	strlen()字串的長度
<br>
</fieldset>
";
echo $message."<br>";
//'5.13'減少輸入的需要
$session_name = $session_txt['5.13'];
echo $session_name."<br>";

function do_function ($option){
	switch ($option){
		case "Sent";
			print "Sent";
			break;
		case "Delete";
			print "Deleted";
			break;
		case "Open Mail";
			print "Mail Opened";
			break;
		case "Read Message";
			print "Message Read";
			break;
		case "Reply-to Message";
			print "Reply-to Message";
			break;
	}
}

$mappings = array ( "S" => "Send",
					"D" => "Delete",
					"R" => "Read Message",
					"T" => "Reply-to message");
					
$input = "do something";
do_function ($mappings [strtoupper (substr ($input,0,1))]);

	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   strtoupper()轉換為大寫<br>
   substr(\$input,0,1)取字串,從0開始,取1個字元
<br>
</fieldset>
";
echo $message."<br>";

PHP-Cookbook-chapter4

程式

//'4.1'使用檔案常數
$session_name = $session_txt['4.1'];
echo $session_name."<br>";

//require_once 'DB.php';

function ferror( $file,
				 $line,
				 $message = 'General Error'){
	$errmsg = "There was an error in script $file,on $line :$message \n";
	error_log($errmsg);
	die ('An error occurred, it has been logged in the system log');
				 }

//$dbh = DB::connect("mysql://user:secret@localhost/dbname");
//if (!$dbh){
	//$dbh = @ferror(__FILE__,__LINE__,sprintf('[%d]:%s'),
	//$dbh->getCode(),$dbh->getMessage());
//}
	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   此函式,它從使用者端接受參數\$file、\$line、和\$message,並<br>
   經過格式美化之後印出,使結果較容易閱讀。我們也為\$message指定<br>
   一個預設值,以便可以忽叫此函式時省略\$message這個參數。
<br>
</fieldset>
";
echo $message."<br>";
//'4.2'PHP的作業系統和版本常數
$session_name = $session_txt['4.2'];
echo $session_name."<br>";

switch (PHP_OS){
	case "WIN32";
		//win32_function();
		break;
	case "Linux";
		//linux_function();
		break;
	case "OS/2";
		//os2_function();
		break;
}

//error_reporting(E_WARNING);
//PHP_OS ="Linux";
if (!defined(PHP_OS)){
	//die ("OS Detection cannot be done automatically");
}
		
	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   當你想寫出一個大小通吃的程式而又不知道作業系統版本時就很有用。
<br>
</fieldset>
";
echo $message."<br>";
//'4.3'用PHP的錯誤常數設定中斷點
$session_name = $session_txt['4.3'];
echo $session_name."<br>";

error_reporting(E_WARNING);
$line = "The \nQuick \nYoung \nProgrammer \nJumped \nOver \nThe
         \nLazy \nOld \nProgrammer";
$line = ereg_replace ("(\n"," ",$line);//mis-matched parentheses
print $line."<br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   在程式裡,ereg_replace(),第一個參數的常規表示少了一個小括號<br>
   。通常PHP不會回報這個錯誤,但因為我們透過error_reporting()<br>
   呼叫PHP回報常規表示的錯誤,所以現在就會收到錯誤的回報。
<br>
</fieldset>
";
echo $message."<br>";
//'4.4'定義自己的PHP常數
$session_name = $session_txt['4.4'];
echo $session_name."<br>";

define ("HI","Hello World");
print HI."<br>";

/*
function four (){
	return 4;
}
define ("HI","four()");
print HI;
*/

if (!defined ("FIVE"))
	@defined ("FIVE","five");
else
	return;

print FIVE;



$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   此程式用defined()函式定義HI為Hello World。
</fieldset>
";
echo $message."<br>";
/'4.5'PHP的全域變數
$session_name = $session_txt['4.5'];
echo $session_name."<br>";
unset ($message);

reset ($GLOBALS);
while (list($key,$var) = each ($GLOBALS)){
	print "$key =>$var \n<br>\n";
}



$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  此範例使用PHP的list()和each()函式來處理\$GLOBALS陣列。
</fieldset>
";
echo $message."<br>";