PHP built-in function example tutorial, built-in function example Tutorial _php tutorial

Source: Internet
Author: User
Tags glob

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


Uppercase and lowercase conversion related functions

Strtolower ()
Strtoupper ()
Ucfirst ()
Ucword ()

Text HTML tag handler function


NL2BR ()
Htmllentities ()
Htmlspecialchars ()
Stripslashes ()
Strip_tags ()
Number_format ()
Strrev ()
MD5 ()


Convert to lowercase
$a = ' www.jb51.net ';
echo strtolower ($a);
Results: Www.jb51.net

Convert to uppercase
$a = ' www.jb51.net ';
echo strtoupper ($a);
Results: WWW.jb51.net

Capitalize first letter
$a = ' www.jb51.net ';
echo Ucfirst ($a);
Results: Www.jb51.net

Capitalize the first letter of each word
$a = ' I love you ';
echo Ucword ($a);
Results: I Love You

/*
Tip: We all know the case, think lowercase and uppercase is different, but why to distinguish between case?
In the win system, PHP size is not strict, but under the Linux system is strictly prohibited to pull large lowercase can not write
Like what
When the class is loaded automatically
function _autoload ($className) {
Include Strtolower ($className). Class.php ';
}
$obj = new MyClass;
This will load the myclass.class.php
Because the file name is usually lowercase, you must convert lowercase
?>
*/

NL2BR to convert a space into an entity
Because the line breaks that are normally displayed in the browser are
For example, in the form of the message book must be converted or the broken line is not successful more space is the task is a space
$a = '
I
Love
You
';
echo $a;
echo nl2br ($a);
Result 1:i Love You
Result 2:
I
Love
You

Form submission If you do not do HTML tag processing then it will directly display the style or JS code directly run
/*

When you enter

Www.jb51.net


A submission appears to be a large font size
But you were trying to

Www.jb51.net

Results of
So to deal with the next
When the input
A commit will run JavaScript
It must be handled to prevent hackers from finding the entrance to your attack.
Form default Submission method is get
*/
When you enter

Www.jb51.net


echo htmlspecialchars ($_get[' title ');//Filter
Results:

Www.jb51.net


Other view source will know that < and > has been replaced by < > will be displayed in the page prototype
There is a point to the main if you do not handle the copy of the article itself has a label style will disrupt your page layout possible CSS conflicts
Htmllentities () function user and htmlspecialchars () the opposite is not said.

When you need that label, you can use the Strip_tags () function.
echo strip_tags ($_get[' title '), '

');
Submit the result is that you look at the source code will find out

/*
Add input I love ' jb51 ';
The result of the submission is I love \ ' jb51\ ' backslash escaped
So what do I have to do with the original output?
You can use this PHP function stripslashes ()
Cancel Escape
echo stripslashes ($_get[' title ');
The result is I love ' jb51 ';
If you have HTML tags like this
I love ' jb51 '
I want the prototype output, how can I use 2 functions to combine with what I've already said
Echo Htmlspecialchars (stripslashes ($_get[' title '));
Result: I love ' jb51 '
*/

Number_format () This function is formatted currency function different countries ' habits are not the same so the need for the currency display is not the same as the mall China money is usually the format of such
Thousands of bits are separated with commas to keep a few dots person ' decimal '
The use of this function is simple
Number_format ($money, the decimal point to keep a few, ' what separates the decimal point ', ' what separates the Thousand Points ')
$price = ' 123465789.233 ';
Echo Number_format ($money, 2, ', ', '. ');
Results: 123.465.789,23
Echo Number_format ($money, 2, '. ', ', ');//Chinese-style
Results: 123,465,789.23

Strrev () the string instead
$str = ' http://www.jb51.net ';
echo Strrev ($STR);
Results: moc.tenwii.www//:p TTH

MD5 is encrypted user name password must be encrypted to prevent hackers
$a = ' admin ';
echo $b = MD5 ($a);

Built-in functions are functions provided by PHP, which can implement many functions Is_int, Is_integer, Is_long, determine whether the variable is integer is_float, is_double, is_real, determine whether the variable is floating-point type Is_ BOOL Determines whether the variable is a Boolean is_string whether the variable is a string Is_array whether the variable is an array type Is_resource determine if the variable is a resource type is_object determine whether the variable is an object is_null determine if the variable is null $_server[' Script_name '] returns/mantis/test.php, relative path;
__FILE__ returns the absolute path of the file D:\Projects\mantis\test.php
$_server[' Http_x_forwarded_proto ']
$_server[' HTTPS ']
$_server[' Server_port ']
$_server["Request_uri"] URI to specify the page to access
$GLOBALS a globally combined array that contains all the variables. The name of the variable is the key of the array.
Explode uses one string to split another string to return an array of strings
Implode to link array elements into strings using one character
$_server[' server_name ' the host name of the server where the current script resides
$_server[' http_host ') domain name
$_server[' server_addr ' The IP address of the server where the current script resides
Trim removes white space characters at the beginning and end of a string
basename returns the file name section
DirName return to the catalog section
krsort-an array in reverse order by key name
Set_time_limit () Setting the maximum execution time of a script

Time () returns the current UNIX timestamp

Is_string () to determine if a variable is a string

Is_bool

Is_inteter

Is_float

Strtotime--Parse the datetime description of any English text into a UNIX timestamp

file_exists checking for file or directory storage not present

fopen () Opens the specified file or URL

Feof () Determines whether the file pointer is at the end

Fgets () reads a row from the file pointer

Trim () remove space before and after a row

Strlen () returns the length of a string

Str_replace
Isset
Str_replace ("+", "', $t _self) Replace the t_self in" "
strpos-find the first occurrence in a string
$_server[' server_software ' Server information
Realpath returns the normalized absolute path name
Directory_separator Operating system directory separators
Path_separator is a ":" Sign on a Linux system, and Windows is a ";" No.
Set_include_path setting Incldue_path

Get_include_path Get current Include_path

COUNT () returns the array length, non-array returns 1

substr (string string, int start [int length]) returns the length of the string starting at the start of the specified position
$_server[' local_addr '] IIS7 to get the server IP address
$_server[' remote_addr ') browser user's IP address
Array_pad-fills an array to a specified length with a value
Memory_get_usage () returns the amount of memory allocated to PHP
Spl_autoload_register Register __autoload () function
Ob_get_contents () Return the contents of the output buffer
Strtolower () converts uppercase letters to lowercase
extension_loaded (' zlib ') to find out if the extension is loaded
Ini_get returns the value of a configuration item in php.ini
Ini_set set the value of the configuration item in PHP.ini, Ini_set ("Memory_limit", ' 256M '); is only valid during script execution and the script execution completes the value reset
Php_eol PHP line Terminator

Preg_replace performs a replacement of a regular expression

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

Htmlspecialchars Convert special characters to HTML entities
Ceil () into a method to take the whole
Floor () de-rounding method
Round () Rounding methodvery useful 10 uncommon PHP built-in functions

1. Sys_getloadavg ()

SYS_GETLOADAVT () can obtain the system load condition. The function returns an array of three elements, each representing the average load for the system in the past 1, 5, and 15 minutes.

Instead of letting the server down because of high load, it is better to die out a script when the system is heavily loaded, Sys_getloadavg () is used to help you achieve this function. Unfortunately, this function is not valid under Windows.


2. Pack ()

Pack () can save storage space by converting the 32-bit 16 binary string returned by MD5 () to a 16-bit binary string.


3. Cal_days_in_month ()

Cal_days_in_month () is able to return the number of days in a specified month.


4. _ ()

WordPress developers often see this function, as well as _e (). The functions of the two functions are the same and are used in conjunction with the GetText () function to enable multilingual Web sites. Refer to the relevant section of the PHP manual for details.


5. Get_browser ()

Is it good to see what the user's browser can do before sending the page? Get_browser () can get the user's browser type, as well as the features supported by the browser, but first you need a Php_browscap.ini file to make a reference file for the function.

It is important to note that the function of the browser is based on the general characteristics of the browser. For example, if a user turns off browser support for JavaScript, the function is not aware of this. However, the function is accurate in judging the browser type and OS platform.

6. Debug_print_backtrace ()

This is a debugging function that can help you find logic errors in your code. To understand this function, just look at an example:

    • $a = 0;
    • function Iterate () {
    • Global $a;
    • if ($a < 10)
    • Recur ();
    • echo $a. “, “;
    • }
    • function recur () {
    • Global $a;
    • $a + +;
    • How do I get here?
    • echo "\n\n\n";
    • Debug_print_backtrace ();
    • if ($a < 10)
    • Iterate ();
    • }
    • Iterate ();
    • ?>
OUTPUT:
#0 recur () called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate () called at [c:\htdocs\php_stuff\index.php:25]
#0 recur () called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate () called at [c:\htdocs\php_stuff\index.php:21]
#2 recur () called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate () called at [c:\htdocs\php_stuff\index.php:25]
#0 recur () called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate () called at [c:\htdocs\php_stuff\index.php:21]
#2 recur () called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate () called at [c:\htdocs\php_stuff\index.php:21]
#4 recur () called at [C:\htdocs\php_stuff\index.php:8]
#5 iterate () called at [c:\htdocs\php_stuff\index.php:25]

7. Metaphone ()

This function returns the Metaphone value of the word, with the same pronounced word having the same Metaphone value, which means that the function can help you determine whether the two words are pronounced the same. But the Chinese will not work.

8. Natsort ()

Natsort () can arrange an array in a natural sort order, looking directly at an example:

    • $items = Array ("Apples", "5 apples", "apples", "apples");
    • Normal sorting:
    • Sort ($items);
    • Print_r ($items);
    • ?>
Outputs:
# Array
# (
# [0] = + apples
# [1] = apples
# [2] = 5 apples
# [3] = apples
# )
Natsort ($items);
Print_r ($items);
# Outputs:
# Array
# (
# [2] = 5 apples
# [3] = apples
# [0] = + apples
# [1] = apples
# )

9. Levenshtein ()

Levenshtein () tells you the "distance" between the two words. It tells you how many letters you need to insert, replace, and delete if you want to turn 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?";
    • ?>

Glob ()

Glob () will make you feel stupid to find files with Opendir (), Readdir () and Closedir ().

    • foreach (Glob ("*.php") as $file) {
    • echo "$file \ n";
    • }
    • ?>

http://www.bkjia.com/PHPjc/1128378.html www.bkjia.com true http://www.bkjia.com/PHPjc/1128378.html techarticle PHP Built-in function instance tutorial, built-in function instance tutorial has case conversion correlation function strtolower () strtoupper () Ucfirst () Ucword () text HTML tag handling function nl2br () Htmllentitie ...

  • Related Article

    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.