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>";