萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php 文本轉HTML與獲取IP函數

php 文本轉HTML與獲取IP函數

php 文本轉HTML與獲取IP函數

/**
 * 文本轉HTML
 *
 * @param string $txt;
 * return string;
 */
function Text2Html($txt){
 $txt = str_replace("  "," ",$txt);
 $txt = str_replace("<","&lt;",$txt);
 $txt = str_replace(">","&gt;",$txt);
 $txt = preg_replace("/[rn]{1,}/isU","<br/>rn",$txt);
 return $txt;
}

/**
 * 獲得IP
 * return string;
 */
function GetIP(){
 if(!empty($_SERVER["HTTP_CLIENT_IP"])) { $cip = $_SERVER["HTTP_CLIENT_IP"]; }
 else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; }
 else if(!empty($_SERVER["REMOTE_ADDR"])) { $cip = $_SERVER["REMOTE_ADDR"]; }
 else $cip = "";
 preg_match("/[d.]{7,15}/", $cip, $cips);
 $cip = $cips[0] ? $cips[0] : 'unknown';
 unset($cips);
 return $cip;
}

copyright © 萬盛學電腦網 all rights reserved