Summary of string manipulation functions in PHP

Source: Internet
Author: User
The following small series for everyone to bring an article based on the PHP string Operation function Collection. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

1. Find the character position function:


Strpos ($str, Search,[int])://Find search first position in $str starting with int; Strrpos ($STR, Search,[int])://Find search in $ The last occurrence in Str starts with an int

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


Submit ($str, int start[,int length])://start extracting [length string] from $str in strat position. Strstr ($str 1, $str 2)://Search for $STR2 from $STR1 (first position) and intercept to end string from it, false if not. STRISTR ()//function with STRSTR, just not case sensitive. STRRCHR ()//prompt returned from the last word searched; use: Fetch the file name in the path

3. Replace string


Str_replace (Search,replace, $str): Find search from $STR replace str_irreplace with replace (Search,replace, $STR): Strtr ($STR, search Replace): Replace cannot be "" In this function, Substr_replace ($STR, $rep, $start [, length]) $str original string, $rep new string after replacement, $start start position, $ Length of the replacement, which is optional

4. Query string length


int strlen ($STR)

5. Compare character functions

int strcmp ($str 1, $str 2): $str 1>=< $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. Dividing the array function

Str_split ($str, Len): Splits the $str by Len length return array split (search, $str [, int]): Splits the $str by the search character return array int is split several times, the back will not be split explode ( 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. Returns the specified character or ASCII

Chr, Ord.

10. HTML code related functions

NL2BR (): Make \ n convert to <br>. Strip_tags ($str [, ' <p> '])://Remove HTML and PHP tags htmlspecialchars ($str [, Parameters])://page normal output HTML code parameter is the conversion mode

11. Character Case Conversion function

Strtolower ($STR) converts a string to lowercase strtoupper ($str) 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 functions

Addslashes ($STR): Causes the single quotation mark ('), double quotation mark ("), backslash (\), and nul string in STR to be converted to \ ', \ ', \ \. MAGIC_QUOTES_GPC = on automatically escapes the contents of the Get post Cookie GET_MAGIC_QUOTES_GPC () detects if Magic_quotes_gpcstripslashes () is turned on to remove backslashes from the string

13. Connection function

Implode (str, $arr) concatenate a string array into a string by a specified character; the implode () function has an individual name function join

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.