PHP-Based String operation functions and php string functions

Source: Internet
Author: User

PHP-Based String operation functions and php string functions

1. Search for the character location function:

Strpos ($ str, search, [int]): // search for the first position of search in $ str starting from int; strrpos ($ str, search, [int]): // search for the last occurrence position of search in $ str starting from int

2. Extract sub-character functions (dual-byte)

Submit ($ str, int start [, int length]): // extract a [length string] starting from the strat position in $ str. Strstr ($ str1, $ str2): // search $ 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 it is case insensitive. Strrchr () // returns from the last searched character. Usage: get the file name in the path

3. Replace string

Str_replace (search, replace, $ str): from $ str, search for search to replace str_irreplace (search, replace, $ str): strtr ($ str, search, replace ): in this function, replace cannot be ""; substr_replace ($ Str, $ rep, $ start [, length]) $ str original string, $ rep new string after replacement, the starting position of $ start and the length of $ length. This parameter is optional.

4. query string length

int strlen($str)

5. Comparison of character Functions

Int strcmp ($ str1, $ str2): $ str1 >=< $ str2 is positive 1, 0, and-1 (string comparison strcasecmp () is the same as above (case-insensitive strnatcmp ("4 ", "14") Comparison string strnatcasecmp () In natural order (same as above, (Case Sensitive)

6. Split into array Functions

Str_split ($ str, len): splits $ str by len Length and returns the array split (search, $ str [, int]): splits $ str by the search character. The returned array 'int' is separated several times, and the following will not be split into explode (search, $ str [, int]).

7. Remove spaces:

Ltrim, rtrim, trim

8. Space Function

Chunk_split ($ str, 2); add a space to the $ str string containing 2 characters;

9. Return the specified character or ascii

Chr, ord

10. functions related to HTML code

Nl2br (): converts \ n to <br>. Strip_tags ($ str [, '<p>']): // remove HTML and PHP tags htmlspecialchars ($ str [, parameter]): // The HTML code parameters output normally on the page are converted.

11. case-insensitive conversion functions

Strtolower ($ str) string to lower case strtoupper ($ str) string to upper case ucfirst ($ str) to convert the first character of the function to upper case ucwords ($ str) converts the first letter of each word to uppercase.

12. Database-related functions

Addslashes ($ str): converts single quotation marks ('), double quotation marks ("), backslash (\), and NUL strings \',\",\\. Magic_quotes_gpc = On automatically escapes the content of the get post cookie. get_magic_quotes_gpc () checks whether magic_quotes_gpcstripslashes () is enabled to remove the backslash from the string.

13. Connection Functions

Implode (str, $ arr) concatenates a string array into a string based on specified characters. The implode () function has an alias function for join.

The above collection of string operation functions self-contained in PHP is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.