Some functions that are often used in PHP development

Source: Internet
Author: User
?
Class useful{
/*
* Common function class
* Author: More than a rookie
* Contact Email: KINGERQ at MSN DOT com
* Date Created: 2005-07-18
* Source: Http://blog.csdn.net/kingerq
*/

/*
* Function: Format numbers, output in standard money format
*/

function FormatNumber ($num) {
Return Number_format ($num, 2, ".", ",");
}

/*
* Function: Format text, turn \ n into <br>
* Parameters: $string Source string
* Return: Processed string
*/
function formatstring ($string = "") {
$string = preg_replace (Array ("//", "///"), Array ("", ""), $string);
Return nl2br ($string);
}

/*
* Function: Format Text output
* parameter $text for text content that needs to be formatted
*/
function FormatContent ($text) {
$trans = get_html_translation_table (html_specialchars);
$trans = Array_flip ($trans);
$text = Strtr ($text, $trans);
$text = Str_replace ("\ n", "<br>", $text);
$text = Str_replace ("", "", $text);
return $text;
}

/*
* Convert bytes to KB or MB
* parameter $num to byte size
*/
function Bitsize ($num) {
if (!preg_match ("/^[0-9]+$/", $num)) return 0;
return $num > 1024? ($num/1024 > 1024 round ($num/1024/1024, 2). " Mb ": Round ($num/1024, 2)." Kb "): $num." Byte ";
}

/*
* Anti-injection processing (add a slash to the variable) function
* Parameter $array is an array of anti-injection variables
*/
Function add_s (& $array) {
foreach ($array as $key => $value) {
if (!is_array ($value)) {
$array [$key]=addslashes ($value);
}else{
$this->add_s ($array [$key]);
}
}
}

/*
* Convert HTML special characters (use when form is submitted, prevent malicious JS code)
* Parameter $array is a string or array to convert
*/
Function specialhtml (& $array) {
if (Is_array ($array)) {//array processing
foreach ($array as $key => $value) {
if (!is_array ($value)) {
$array [$key]=htmlspecialchars ($value);
}else{
$this->specialhtml ($array [$key]);
}
}
}else{
$array = Htmlspecialchars ($array);
}
}

/*
* Can avoid garbled interception of Chinese characters
* Parameter $str is a string, $start is a start character, $len end character
* Returns the intercepted character
*/
function Msubstr ($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for ($i = 0; $i < $strlen; $i + +) {
if (Ord (substr ($str, $i, 1)) > 0xa0) {
$tmpstr. = substr ($str, $i, 2);
$i + +;
} else
$tmpstr. = substr ($str, $i, 1);
}
return $tmpstr;
}

/*
* Function: Comprehensive hint JS code output
* Parameter $msg to hint information
* $direct for prompt type 0 for prompt (default) 1 for prompt refresh return 2 for prompt return
* Output prompt code and end program
*/
function alert_msg ($msg, $direct = "0") {
Switch ($direct) {
Case ' 0 '://Prompt
$script = "";
Case ' 1 '://Prompt Refresh return
$script = "Location.href=\" "$_server[" Http_referer "]." \";";
Break
Case ' 2 '://Prompt return
$script = "History.back ();";
Break
default://tip to the specified page
$script = "Location.href=\" ". $direct." \";";
}
echo "<script language= ' JavaScript ' >window.alert ('. $msg. ');". $script. " </script> ";
Exit
}

/*
* Function: Get start date and end date of week with given date
* Parameters: $gdate date, default for the same day, format: YYYY-MM-DD
* $first Week in Monday or Sunday, 0 for Sunday, 1 for Monday
* Return: Array ("Start date", "End Date");
*/
function Aweek ($gdate = "", $first = 0) {
if (! $gdate) $gdate = Date ("y-m-d");
$w = Date ("W", Strtotime ($gdate));//Get the first day of the week, Sunday start 0-6
$dn = $w? $w-$first: 6;//number of days to subtract
$st = Date ("y-m-d", Strtotime ("$gdate-". $dn. "Days"));
$en = Date ("y-m-d", Strtotime ("$st +6 Days"));
Return Array ($st, $en);/back to start and end dates
}

/*
* Function: Check whether the page is legally connected.
* If it's illegal, turn to the landing window
*/
function Checkurl () {
If you connect to the page directly from the browser, connect to the login window
echo "Referer:". $_server[' Http_referer '];
if (!isset ($_server[' http_referer ')) {
Header ("location:index.php");
Exit
}
$urlar = Parse_url ($_server[' http_referer '));
If the domain name of the page is not a server domain, connect to the login window
if ($_server["Http_host"]!= $urlar ["HOST"]) {
Header ("location:index.php");
Exit
}
}

/*
* Read the contents of the file
* parameter $file as filename and full path
* Return the contents of the file
*/
function Readfiles ($file) {
$tdata = "";
$fp = fopen ($file, "R");

if (FileSize ($file) <= 0) return;

while ($data = Fread ($fp, FileSize ($file))) {
$tdata. = $data;
}
Fclose ($FP);
return $tdata;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.