[Recommended] Summary of various PHP string functions

Source: Internet
Author: User
PHP string functions include the character location search function, the sub-character extraction function, the replacement string, the character length, the comparison character function, the array character, and the space.

String functions in PHP are also easy to understand. Today, we have summarized nearly 12 PHP string functions for you, hoping to help other users and increase their PHP knowledge base.

 

 

1. character location search function:

Strpos ($ STR, search, [int]): Search for the first position of search in $ STR starting from Int;

Stripos ($ STR, search, [int]):The function returns the position where the string first appears in another string. This function is case insensitive.

Strrpos ($ STR, search, [int]):Search for the last occurrence position of search in $ STR starting from Int.

Strripos ($ STR, search, [int]):Same as above. This function is case insensitive.

2. Extract the sub-string function (dual-byte)

Substr ($ STR, int start [, int length]):Extract A [length string] From the Strat position in $ Str.

Strstr ($ str1, $ str2 ):Search for $ str2 from $ str1 (the first position) and extract the string from it to the end. If not, false is returned.

Stristr ()The function is the same as that of strstr, but not case sensitive.

Strrchr ()Returns from the last searched character. Usage: obtains the file name in the path.

3. php string functions that replace strings

Str_replace (search, replace, $ Str ):Search for search from $ STR and replace it with replace

Str_ireplace (search, replace, $ Str ):Same as above. This function is case insensitive.

Strtr ($ STR, search, replace ):In this function, replace cannot be "";

Substr_replace ($ STR, $ rep, $ start [, length]):$ STR original string, $ new string after rep replacement, $ start position, $ length replacement length, optional

4. Character Length

Int strlen ($ Str)

5. Comparison of character Functions

Int strcmp ($ str1, $ str2 ):$ Str1 >=< $ str2 is positive 1, 0,-1 (string comparison), respectively)

Strcasecmp ()Same as above (Case Insensitive)

Strnatcmp ("4", "14 ")Sort and compare strings by nature

Strnatcasecmp ()Same as above (case sensitive)

6. php string functions split into Arrays

Str_split ($ STR, Len ):Splits $ STR by LEN Length and returns an array.

Split (search, $ STR [, int]):Splits $ STR by the search character. The returned array 'int' is separated several times, and the following strings are not separated.

Expload (search, $ STR [, int])

7. Remove spaces:

Ltrim (), rtrim (), trim ()

8. Space Function

Chunk_split ($ STR, 2 ):Add a space to the $ STR character based on 2 characters;

9. CHR and ord -- return the specified character or ASCII

10. htmlCodeRelated functions

Nl2br ():Convert \ n to <br>.

Strip_tags ($ STR [, '<p>']):Remove HTML and PHP tags

In $ STR, all HTML and PHP code will be removed. The optional parameters are HTML and PHP code.

Code written by optional parameters.

For example, Echo strip_tags ($ text, '<br> <p> ');

Htmlspecialchars ($ STR [, parameter]):HTML code parameters are output normally on the page, which is a conversion method.

11. php string functions for case-insensitive character conversion

Strtolower ($ Str ):String to lowercase

Strtoupper ($ Str ):Convert string to uppercase

Ucfirst ($ Str ):Converts the first character of a function to uppercase.

Ucwords ($ Str ):Converts the first letter of each word to uppercase.

12. Database-related PHP string functions

Addslashes ($ Str ):Make single quotation marks ('), double quotation marks ("), backslash (\), and NUL

String \',\",\\.

Magic_quotes_gpc = on: Automatically escape get, post, and cookie content

Get_magic_quotes_gpc ():Check whether magic_quotes_gpc is enabled

Stripslashes ():Removes the backslash from the string.

 

 

 

 

 

 

 

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.