程式
<?php
//1.1字串剖析 strpos()
$email = 'sterling@php.net';
$user_name = substr ($email,0,strpos($email,'@'));
echo $user_name."<br>";
//1.1字串剖析 unpack()
$str = "Gumbi and PDOC are smelly";
$names = unpack("A5name1/x5/A4name2", $str);
echo $names['name1']."<br>".$names['name2']."<br>";
//1.1字串剖析 unpack()
$str2 = 'A box without hinges, key, or lid, Yet golden treasure inside is hid';
$results = unpack ("A1pronoun/x5/A7adjective/X11/A3noun", $str2);
foreach($results as $key => $value){
echo "{$key}: =>{$value} <br>";
}
//1.3交換變數 list()
$items = array ( "Linux",
"Apache",
"PHP",
"A SQL Server",
"Talented Administrator");
print "The essentials for operating your own webserver:\n";
reset ($items);
while (list (, $item) = each ($items)){
print "$item\n";
}
//1.4將ASCII碼轉換為字元
$letter = chr(67);
$ascii_code = ord ($letter);
$letter = sprintf ("%c",$ascii_code);
echo "<br>{$letter}<br>";
//1.5劃分字元
function str_ord ($str){
for ($i = 0; $i < strlen ($str); $i++){
$ascii_values[$i] = ord ($str[$i]);
}
return $ascii_values;
}
$str = "Hello World";
$ascii_values = str_ord ($str);
print "The third letter of \$str, a-> $str[2],";
print "has an ASCII value of $ascii_values[2].<br>";
$ascii_values = unpack ("C*char", $str);
//key char
$ascii_values2 = array_values (unpack("C*char", $str));
//轉換key 0,1...
foreach($ascii_values as $key => $value){
echo "{$key}: =>{$value} <br>";
}
foreach($ascii_values2 as $key => $value){
echo "{$key}: =>{$value} <br>";
}
//1.6 反轉部分字串 array_reverse是反轉單字字串 strrev是反轉字元
function word_reverse ($str){
return implode ("",array_reverse (preg_split ("/\s+/",$str)));
}
$str = "A ross by any other name ";
print $str."<br>";
$str_reversed = word_reverse($str);
print $str_reversed."<br>";
$chart_reverse = strrev ($str);
print $chart_reverse."<br>";
//1.8 移除字串中的空白 chop 去除尾部空白 ltrim 去除開頭空白 trim 去除頭尾空白
$str = "red apple " ;
$str = chop ($str);
echo $str."abc<br>";
$str = " red apple " ;
$str = ltrim ($str);
echo "abc".$str."abc<br>";
//1.11 解析URL
$session_name = "1.11 解析URL";
echo $session_name."<br>";
$url = 'http://www.php.net/search.php?show=nource&';
$url .='patter=parse_url&sourceurl=http%3A%2F%2Fwww.php.net%2F ';
$url_parts = parse_url ($url);
foreach ($url_parts as $key =>$value ){
echo "{$key} => {$value} <br>";
}
//1.14 對字串加密
$session_name = "1.14 對字串加密 支援md5() base64_encode() base64_decode()";
echo $session_name."<br>";
/* 無法支援mcrypt
$key = "Sterling Hughes ";
$string = "Hello World" ;
$encrypted = mcrypt_ecb (MCRYPT_TripleDES, $key ,$string, mcrypt_encrypt);
$decrypted = mcrypt_ecb (MCRYPT_TripleDES, $key ,$string, mcrypt_decrypt);
echo $encrypted."<br>";
echo $decrypted."<br>";
*/
/*
$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
$key_size = strlen($key);
echo "key size: ".$key_size."<br>";
$string = "Hello World" ;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
$string, MCRYPT_MODE_CBC, $iv);
$ciphertext = $iv . $ciphertext;
$ciphertext_base64 = base64_encode($ciphertext);
echo $ciphertext_base64 . "\n";
*/
$data = "Thanks for this idea Zak!";
$hashed_data = md5 ($data);
print "The md5 hash of $data is $hashed_data ";
$data = "Hello World";
$hashed_data = md5 ($data);
print "The md5 hash of $data is $hashed_data <br> ";
$base63_data = base64_encode ($data);
print "The base64_encode hash of $data is $base63_data <br> ";
$decode_data = base64_decode($base63_data);
print "The base64_decode hash of $base63_data is $decode_data <br> ";