Summary of various PHP string functions

Source: Internet
Author: User

1. character location search function:

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

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

Strrpos ($ str, search, [int]): searches for the last position of search in $ str from int

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

Submit ($ str, int start [, int length]): Extracts [length strings] starting from the strat position in $ str.

Strstr ($ str1, $ str2): searches for $ str2 from $ str1 (the first position) and truncates the string from the start to the end. If not, FALSE is returned.

Stristr () is the same as strstr, but is case insensitive.

Strrchr () is returned from the last searched character. Usage: get 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_irreplace (search, replace, $ str ):

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

Substr_replace ($ Str, $ rep, $ start [, length]) $ str original string, $ new after rep replacement

String, starting position of $ start, and length of $ length. Optional.

4. Character Length

Int strlen ($ str)

5. Comparison of character Functions

Int strcmp ($ str1, $ str2): $ str1 & gt ;=< $ str2 is positive 1, 0,-1 (string comparison)

Strcasecmp () Same as above (case-insensitive)

Strnatcmp ("4", "14") compares strings in natural order

Strnatcasecmp () is the 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 and returns the number of times the array int is split.

Expload (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. chr and ord -- return the specified character or ascii

10. functions related to HTML code

Nl2br (): converts n to <br>.

Strip_tags ($ str [, '<p>']): removes 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]): the HTML code parameters output normally on the page are converted.

11. PHP string functions for case-insensitive character conversion

Strtolower ($ str) string to lowercase

Strtoupper ($ str) string to uppercase

Ucfirst ($ str) converts the first character of the function to uppercase.

Ucwords ($ str) converts the first letter of each word to uppercase

12. Database-related PHP string functions

Addslashes ($ str): enables single quotation marks ('), double quotation marks ("), backslash (), and NUL in str.

String ',",\.

Magic_quotes_gpc = On automatically escapes the get post 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.