PHP string Common functions

Source: Internet
Author: User
Tags php language knowledge base

String functions in the PHP language are also a relatively understandable knowledge. Today we summed up nearly 12 kinds of PHP string functions, I hope that the need for friends to help, increase the reader friend's PHP Knowledge base.

1. Find 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 the last occurrence of search in $str from int

2. Extracting sub-character functions (double-byte)

Submit ($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_irreplace (Search,replace, $STR):

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

Substr_replace ($Str, $rep, $start [, length]) $str The original string, $rep replace the new

The string, $start start 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&gt;=< $str 2 are positive 1,0,-1 (string comparison)

STRCASECMP () ditto (no case)

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

STRNATCASECMP () ibid. (case sensitive)

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

Expload (Search, $str [, int])

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 convert to &lt;br>.

Strip_tags ($str [, ¹<p>¹]): 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, ¹<br><p>¹);

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 () to remove backslashes from a string

PHP 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.