Php basic tutorial php built-in function instance tutorial

Source: Internet
Author: User
In fact, this content can be learned in the php manual, but many beginners of php do not know how to look at the php manual, so I love the Micro Network. now I will explain the first php built-in functions.
Case-sensitive conversion functions
Text html tag processing function

Case-sensitive functions
Copy codeThe code is as follows:
Strtolower ()
Strtoupper ()
Ucfirst ()
Ucword ()


String formatting functions related to HTML tags
Copy codeThe code is as follows:
Nl2br ()
Htmllentities ()
Htmlspecialchars ()
Stripslashes ()
Strip_tags ()
Number_format ()
Strrev ()
Md5 ()


In php, all string processing functions are not modified on the original string, but return a newly formatted string.
Copy codeThe code is as follows:
// Convert to lowercase
$ A = 'www .bitsCN.com ';
Echo strtolower ($ );
// Result: www.bitsCN.com

// Convert to uppercase
$ A = 'www .bitsCN.com ';
Echo strtoupper ($ );
// Result: WWW.bitsCN.com

// Uppercase letters
$ A = 'www .bitsCN.com ';
Echo ucfirst ($ );
// Result: Www.bitsCN.com

// Uppercase letters of each word
$ A = 'I love you ';
Echo ucword ($ );
// Result: I Love You

/*
Tip: We all know the case sensitivity. do you think the case sensitivity is different from the case sensitivity and the case sensitivity?
In Windows, php is not case sensitive, but cannot be case insensitive in linux.
For example
When the class is automatically loaded
Function _ autoload ($ className ){
Include strtolower ($ className). '. class. php ';
}
$ Obj = new MyClass;
In this way, myclass. class. php will be loaded.
Because the file name is often lowercase, it must be converted to lowercase.
?>
*/

// Nl2br converts spaces into entities
Because the line feed shown in the browser is usually
For example, in the form message book, it must be converted. Otherwise, if the line cannot be broken, more spaces will be processed as a space.
$ A ='
I
Love
You
';
Echo $;
Echo nl2br ($ );
Result 1: I love you.
Result 2:
I
Love
You

// If you do not perform html tag processing for form submission, the style or js code will be displayed and run directly.
/*


When you enter
Www.bitsCN.com
A large font is displayed upon submission.
But you wanted
Result of www.bitsCN.com
So proceed
When you enter script alert ('www .bitsCN.com ') script
Javascript will be run once submitted
If this is not the case, you have to deal with it to prevent hackers from finding the entrance to your attack.
The default form submission method is get.
*/
// When you enter www.bitsCN.com
Echo htmlspecialchars ($ _ GET ['title']); // filtered
Result: www.bitsCN.com
When you view the source code, you will know that <and> have been replaced with <& gt, and the page prototype is displayed.
Another point is to avoid css conflicts if you do not have to deal with the tag style of some copied articles.
Htmllentities () function user and htmlspecialchars () are not used in the opposite way.

The strip_tags () function can be used when you need to keep the tag.
Echo strip_tags ($ _ GET ['title'],'

');
If you view the source code, you will find that the source code is missing.

/*
Add the input I love 'jb51 ';
The submission result is I love \ 'jb51 \ '. the backslash is escaped.
What should I do if I want to output the original text?
You can use this php function stripslashes ()
Cancel escape
Echo stripslashes ($ _ GET ['title']);
The result is I love 'jb51 ';
What if html tags are contained?
I love 'jb51'
What should I do with prototype output? I can combine two functions to use what I have already said.
Echo htmlspecialchars (stripslashes ($ _ GET ['title']);
Result: I love 'jb51'
*/

// Number_format () this function is used to format the currency function. different countries have different habits, so the display of the required currency is different. for example, the Chinese money in the mall is usually in this format.
Use commas (,) to retain the number of decimal places'
This function is easy to use.
Number_format ($ money, which indicates the number of decimal points reserved. 'What are the decimal points separated 'and 'What are the decimal points separated ')
$ Price = '1970. 100 ';
Echo number_format ($ money, 2 ,',','.');
Result: 123.465.789, 23
Echo number_format ($ money, 2, '.', ','); // Chinese
Result: 123,465,789.23

// Strrev () returns the string
$ Str = 'http: // www.bitsCN.com ';
Echo strrev ($ str );
Result: moc. tenwii. www //: ptth

// Md5 indicates that the user name and password must be encrypted to prevent hackers.
$ A = 'admin ';
Echo $ B = md5 ($ );

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.