PHP handles string common functions

Source: Internet
Author: User

1 Find the character position function:

Strpos ($str, Search,[int]): Find the first position of search in $str starting with int;

Stripos ($str, search,[int]): function returns the position of the first occurrence of a string in another string. This function is not sensitive to case

Strrpos ($str, Search,[int]): Find where the last occurrence of search in $STR starts with int

Strripos ($str, Search,[int]): Ibid., this function is insensitive to case

2. Extract substring function (double byte)

SUBSTR ($str, int start[,int length]): Extracts [length string] from strat position in $str.

Strstr ($str 1, $str 2): Searches for $str2 from $STR1 (the first position) and intercepts the end string from it, or false if none.

The Stristr () function is the same as STRSTR, but is not case sensitive.

STRRCHR () prompt returned from the last word searched; use: Fetch the file name in the path

3. PHP string function to replace string

Str_replace (Search,replace, $str): Find search from $STR Replace with replace

Str_ireplace (Search,replace, $str): ibid., this function is insensitive to case

STRTR ($str, search,replace): Replace in this function cannot be "";

Substr_replace ($Str, $rep, $start [, Length]): $str The original string, $rep the new string after replacement, $start the starting position, $length the length of the replacement, which is optional

4. Character length

int strlen ($STR)

5. Compare character functions

int strcmp ($str 1, $str 2): $str 1<=< str-p>

STRCASECMP () ditto (no case)

STRNATCMP ("4", "14") compare strings by natural sort

STRNATCASECMP () ibid. (case insensitive)

6. PHP String functions for splitting into groups

Str_split ($str, Len): Splits the $STR by Len length returns the array

Split (search, $str [, int]): Splits the $str by the search character returns the array int is split several times, the back will not be split

Explode (search, $str [, int]) splits the $str by the search character return array int is split several times, the back will not be split

7, remove the space:

LTrim (), RTrim (), Trim ()

8, add the Space function

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

9, CHR, ord--returns the specified character or ASCII

10. HTML code related functions

NL2BR (): Make \ n convert to

Strip_tags ($str [, '

‘]):

Remove HTML and PHP tags

In $str all HTML and PHP code will be removed, and the optional parameters for HTML and PHP code will be preserved

The code that is written for the optional parameter.

such as: Echo Strip_tags ($text, '

‘);

Htmlspecialchars ($str [, Parameters]): page normal output HTML code parameter, is the conversion mode

11. Character-case-conversion PHP String functions

Strtolower ($STR): string converted to lowercase

Strtoupper ($STR): string converted 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): Makes the single quotation mark ('), double quotation mark ("), backslash (\) and NUL in Str

The string is converted to \ ', \ ', \ \.

MAGIC_QUOTES_GPC = On: Automatically escapes the contents of the Get,post,cookie

GET_MAGIC_QUOTES_GPC (): Detects if MAGIC_QUOTES_GPC is turned on

Stripslashes (): Remove backslash from string

PHP handles string common functions

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.