PHP 是一種 HTML 內嵌式的語言,是一種在服務器端執行的`嵌入HTML文件的指令碼語言,語言的風格有類似於C語言,被廣泛的運用。以下是本站小編搜尋整理的關於最實用的PHP例項程式碼21個,供參考借鑑,希望對大家有所幫助!想了解更多相關資訊請持續關注我們應屆畢業生考試網!
1. PHP可閱讀隨機字串
此程式碼將建立一個可閱讀的字串,使其更接近詞典中的單詞,實用且具有密碼驗證功能。
/**************
*@length - length of random string (must be a multiple of 2)
**************/
function readable_random_string($length = 6){
$conso=array("b","c","d","f","g","h","j","k","l",
"m","n","p","r","s","t","v","w","x","y","z");
$vocal=array("a","e","i","o","u");
$password="";
srand ((double)microtime()*1000000);
$max = $length/2;
for($i=1; $i<=$max; $i++)
{
$password.=$conso[rand(0,19)];
$password.=$vocal[rand(0,4)];
}
return $password;
}
2. PHP生成一個隨機字串
如果不需要可閱讀的字串,使用此函式替代,即可建立一個隨機字串,作為使用者的隨機密碼等。
/*************
*@l - length of random string
*/
function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++) {
$rand.= $c[rand()%strlen($c)];
}
return $rand;
}
3. PHP編碼電子郵件地址
使用此程式碼,可以將任何電子郵件地址編碼為 html 字元實體,以防止被垃圾郵件程式收集。
function encode_email($email=’’, $linkText=’Contact Us’, $attrs =’class="emailencoder"’ )
{
// remplazar aroba y puntos
$email = str_replace(’@’, ’@’, $email);
$email = str_replace(’.’, ’.’, $email);
$email = str_split($email, 5);
$linkText = str_replace(’@’, ’@’, $linkText);
$linkText = str_replace(’.’, ’.’, $linkText);
$linkText = str_split($linkText, 5);
$part1 = ’
$part2 = ’ilto:’;
$part3 = ’" .="" attrs="">’;
$part4 = ’’;
$encoded = ’
script type="text/javascript"’;$encoded .= "e(’$part1’);";
$encoded .= "e(’$part2’);";
foreach($email as $e)
{
$encoded .= "e(’$e’);";
}
$encoded .= "e(’$part3’);";
foreach($linkText as $l)
{
$encoded .= "e(’$l’);";
}
$encoded .= "e(’$part4’);";
$encoded .= ’/script