Home > Backend Development > PHP Tutorial > PHP built-in function example tutorial, built-in function example tutorial_PHP tutorial

PHP built-in function example tutorial, built-in function example tutorial_PHP tutorial

WBOY
Release: 2016-07-12 08:51:42
Original
1318 people have browsed it

php内置函数实例教程,内置函数实例教程

有大小写转换相关函数 

strtolower() 
strtoupper() 
ucfirst() 
ucword()

文本html标签处理函数


nl2br() 
htmllentities() 
htmlspecialchars() 
stripslashes() 
strip_tags() 
number_format() 
strrev() 
md5()


//转换成小写
$a='www.jb51.net';
echo strtolower($a);
//结果:www.jb51.net

//转换成大写
$a='www.jb51.net';
echo strtoupper($a);
//结果:WWW.jb51.net

//首字母大写
$a='www.jb51.net';
echo ucfirst($a);
//结果:Www.jb51.net

//每个单词首字母大写
$a='i love you';
echo ucword($a);
//结果:I Love You

/*
提示:大家都知道大小写,认为小写和大写有区别吗,但是为什么要区分大小写呢
在win系统下php大小写不严格 但是在linux系统下就严禁拉 大小写不能乱写
比如
在自动加载类的时候
function _autoload($className){
include strtolower($className).'.class.php';
}
$obj= new MyClass;
这样就加载myclass.class.php
因为文件名常是小写那么必须转换小写
?> 
*/

//nl2br把空格转换成实体
因为一般在浏览器里显示的换行都是
例如在表单留言本里必须要转换不然折行不成功 再多的空格都任务是一个空格 
$a=' 

love 
you 
'; 
echo $a; 
echo nl2br($a); 
结果1:i love you 
结果2: 

love 
you

//表单提交如果你不进行html标签处理那么就会直接显示样式或者js代码直接运行 
/* 

 
 
 

当你输入 

www.jb51.net

 
一提交就出现是一号大字体 
但是你原来是想要 

www.jb51.net

结果的 
所以要处理下 
当输入<script>alert('www.jb51.net')</script> 
一提交就会运行javascript 
这样不好 必须处理下来防止黑客找到你攻击的入口 
表单默认提交方式是get 
*/ 
//当你输入

www.jb51.net

 
echo htmlspecialchars($_GET['title']);//过滤了 
结果:

www.jb51.net


Others, if you check the source code, you will know that < and > have been replaced by < > and it will be displayed in the page prototype.
There is another important point if you do not process
some copied articles Having tag styles will disrupt your page layout and may conflict with css
I won’t talk about the opposite usage of htmllentities() function and htmlspecialchars()

When you need to keep that tag, you can use the strip_tags() function
echo strip_tags($_GET['title'],'

') ;
The submission result is that if you check the source code, you will find that
is gone

/*
Add the input i love 'jb51';
The submission result is i love 'jb51' with the backslash escaped
So what should I do if I want to output the original text?
It can be used This php function stripslashes()
unescapes
echo stripslashes($_GET['title']);
The result is i love 'jb51';
What if it contains html tags like this
i love 'jb51'
What should I do if I want prototype output? I can use two functions combined. I have Said
echo htmlspecialchars(stripslashes($_GET['title']));
Result: i love 'jb51'
*/

//number_format() This function is a formatting currency function. Different countries have different habits, so the required currency display is different. For example, Chinese money in the mall is usually formatted like this.
Use commas to separate thousandths and keep the number. The digit is called the 'decimal point'
The usage of this function is very simple
number_format($money, how many decimal points to keep, 'what to separate the decimal point', 'what to separate the thousandths')
$price ='123465789.233';
echo number_format($money,2,',','.');
Result: 123.465.789, 23
echo number_format($money,2,'.', ',');//Chinese style
Result: 123,465,789.23

//strrev() reverses the string
$str='http://www.jb51.net';
echo strrev($str);
Result: moc.tenwii. www//:ptth

//md5 is encryption. Username and password must be encrypted to prevent hackers.
$a='admin';
echo $b= md5($a);

Built-in functions are functions provided by PHP. Many functions can be achieved through these functions. is_int, is_integer, is_long, determine whether the variable is an integer is_float, is_double, is_real, determine whether the variable is a floating point type is_bool determines whether the variable is of Boolean type is_string determines whether the variable is a string is_array determines whether the variable is an array type is_resource determines whether the variable is a resource type is_object determines whether the variable is an object is_null determines whether the variable is null ​ $_SERVER['SCRIPT_NAME'] returns /mantis/test.php, relative path;
__FILE__ returns the absolute path of the file D:Projectsmantistest.php
$_SERVER['HTTP_X_FORWARDED_PROTO']
$_SERVER['HTTPS ']
$_SERVER['SERVER_PORT']
$_SERVER["REQUEST_URI"] URI is used to specify the page to be accessed
$GLOBALS is a global combination array containing all variables. The name of the variable is the key of the array.
explode uses a string to split another string and returns a string array
implode uses a character to link the array elements into a string
$_SERVER['SERVER_NAME'] The host name of the server where the current script is located
$_SERVER['HTTP_HOST'] domain name
$_SERVER['SERVER_ADDR'] IP address of the server where the current script is located
trim removes the blank characters at the beginning and end of the string
basename returns the file name part
dirname returns the directory part
krsort — sorts the array in reverse order by key name
set_time_limit() sets the maximum execution time of the script

time() returns the current unix timestamp

is_string() determines whether the variable is a string

is_bool

is_Inteter

is_float

strtotime -- Parse the date and time description of any English text into a UNIX timestamp

file_exists checks whether the file or directory exists

fopen() opens the specified file or URL

feof() determines whether the file pointer is at the end

fgets() reads a line from the file pointer

trim() removes spaces before and after a line

strlen() returns the length of the string

str_replace
isset
str_replace( "
get_include_path Get the current include_path

count() returns the length of the array, non-array returns 1
substr(string string,int start [int length]) Returns the length of the string starting from the position specified by start
$_SERVER['LOCAL_ADDR'] Used on IIS7 to obtain the server IP address
$_SERVER['REMOTE_ADDR' ] The IP address of the browser user
array_pad — pads the array with values ​​to the specified length
memory_get_usage() Returns the amount of memory allocated to PHP
spl_autoload_register registers the __autoload() function
ob_get_contents() Return the contents of the output buffer
strtolower() Convert uppercase letters to lowercase letters
extension_loaded('zlib') Find out whether the extension is loaded
ini_get Returns the value of the configuration item in php.ini
ini_set sets the value of the configuration item in php.ini, ini_set("memory_limit",'256M'); only takes effect during script execution. The value is reset after the script is executed
PHP_EOL PHP line terminator

preg_replace performs a regular expression replacement

preg_match(string pattern, string subject [, array matches [, int flags]]) -- Regular expression matching

htmlspecialchars Convert special characters to HTML entities
ceil() Rounding method
floor() Rounding method
round() Rounding method ​ 10 Very Useful Uncommon PHP Built-in Functions

1. sys_getloadavg()

sys_getloadavt() can obtain the system load status. This function returns an array containing three elements, each element representing the system's average load over the past 1, 5, and 15 minutes.

Rather than letting the server crash due to excessive load, it is better to actively die a script when the system load is very high. sys_getloadavg() is used to help you achieve this function. Unfortunately, this function is not valid under Windows.


2. pack()

Pack() can convert the 32-bit hexadecimal string returned by md5() into a 16-bit binary string, which can save storage space.


3. cal_days_in_month()

cal_days_in_month() can return the number of days in the specified month.


4. _()

WordPress developers often see this function, as well as _e(). These two functions have the same function and can be used in combination with the gettext() function to achieve multilingualization of the website. For details, please refer to the relevant parts of the PHP manual.


5. get_browser()

Wouldn’t it be nice to see what the user’s browser can do before sending the page? get_browser() can get the user's browser type and the functions supported by the browser, but first you need a php_browscap.ini file to serve as a reference file for the function.

It should be noted that this function’s judgment of browser functions is based on the general characteristics of this type of browser. For example, if the user has turned off JavaScript support in the browser, the function has no way of knowing this. However, this function is still very accurate in determining browser type and OS platform.

6. debug_print_backtrace()

This is a debugging function that can help you find logical errors in the code. To understand this function, let’s look at an example:

  • $a = 0;
  • function iterate() {
  • global $a;
  • if( $a < 10 )
  • recur();
  • echo $a . “, “;
  • }
  • function recur() {
  • global $a;
  • $a ;
  • //how did I get here?
  • echo “nnn”;
  • debug_print_backtrace();
  • if( $a < 10 )
  • iterate();
  • }
  • iterate();
  • ?>
OUTPUT: 
#0 recur() called at [C:htdocsphp_stuffindex.php:8]
#1 iterate() called at [C:htdocsphp_stuffindex.php:25]
#0 recur() called at [C:htdocsphp_stuffindex.php:8]
#1 iterate() called at [C:htdocsphp_stuffindex.php:21]
#2 recur() called at [C:htdocsphp_stuffindex.php:8]
#3 iterate() called at [C:htdocsphp_stuffindex.php:25]
#0 recur() called at [C:htdocsphp_stuffindex.php:8]
#1 iterate() called at [C:htdocsphp_stuffindex.php:21]
#2 recur() called at [C:htdocsphp_stuffindex.php:8]
#3 iterate() called at [C:htdocsphp_stuffindex.php:21]
#4 recur() called at [C:htdocsphp_stuffindex.php:8]
#5 iterate() called at [C:htdocsphp_stuffindex.php:25]
 
OUTPUT: #0 recur() called at [C:htdocsphp_stuffindex.php:8]#1 iterate() called at [C:htdocsphp_stuffindex .php:25]#0 recur() called at [C:htdocsphp_stuffindex.php:8]#1 iterate() called at [C:htdocsphp_stuffindex.php:21]#2 recur( ) called at [C:htdocsphp_stuffindex.php:8]#3 iterate() called at [C:htdocsphp_stuffindex.php:25]#0 recur() called at [C:htdocsphp_stuffindex.php:8] #1 iterate() called at [C:htdocsphp_stuffindex.php:21]#2 recur() called at [C:htdocsphp_stuffindex.php:8]#3 iterate() called at [C :htdocsphp_stuffindex.php:21]#4 recur() called at [C:htdocsphp_stuffindex.php:8]#5 iterate() called at [C:htdocsphp_stuffindex.php:25] td>

7. metaphone()

This function returns the metaphone value of the word. Words with the same pronunciation have the same metaphone value, which means this function can help you determine whether the pronunciation of two words is the same. But it is invalid for Chinese.

8. natsort()

natsort() can arrange an array in natural sorting. Let’s take a look at an example:

  • $items = array(“100 apples”, “5 apples”, “110 apples”, “55 apples”);
  • // normal sorting:
  • sort($items);
  • print_r($items);
  • ?>
Outputs: 
# Array
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
# )
natsort($items);
print_r($items);
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
# )
Outputs:


# Array
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
# )
natsort($items);
print_r($items);
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
# )

9. levenshtein()

Levenshtein() tells you the "distance" between two words. It tells you how many letters you need to insert, replace, and delete if you want to change one word into another. Let’s look at an example:
  • $dictionary = array(“php”, “javascript”, “css”);
  • $word = “japhp”;
  • $best_match = $dictionary[0];
  • $match_value = levenshtein($dictionary[0], $word);
  • foreach($dictionary as $w) {
  • $value = levenshtein($word, $w);
  • if( $value < $match_value ) {
  • $best_match = $w;
  • $match_value = $value;
  • }
  • }
  • echo ”Did you mean the ‘$best_match’ category?”;
  • ?>

10. glob()

glob() will make you feel stupid using opendir(), readdir() and closedir() to find files.
  • foreach (glob(“*.php”) as $file) {
  • echo “$filen”;
  • }
  • ?>

http://www.bkjia.com/PHPjc/1128378.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1128378.htmlTechArticlephp built-in function example tutorial, the built-in function example tutorial has case conversion related functions strtolower() strtoupper() ucfirst( ) ucword() text html tag processing function nl2br() htmllentitie...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template