Discussion on summarizing and analyzing various PHP string functions _php skills

Source: Internet
Author: User
Tags php language knowledge base

The PHP string function includes the lookup character position function, extracts the child character function, replaces the string, the character length, compares the character function, splits the array character, removes the space and so on.

The string function in the PHP language is also a fairly understandable knowledge. Today we have summed up nearly 12 kinds of PHP string functions, hope to need to help friends, to 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 from int;
Stripos ($str, Search,[int]):function to return the first occurrence of a string in another string. This function is not sensitive to caseStrrpos ($str, Search,[int]):Find out where the last occurrence of search in $STR begins with int
Strripos ($str, Search,[int]):Ditto, 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 the $str2 from the $STR1 (the first position) and intercepts it to the ending string, or false if not.
The STRISTR () feature is the same as STRSTR, but is case-insensitive.
STRRCHR () return from the last search to the word entered prompt; use: take the filename in the path

3, the substitution string PHP string function
Str_replace (Search,replace, $STR): Search from $STR Replace with replace
Str_ireplace (Search,replace, $str): Ibid., this function is insensitive to case
STRTR ($str, search,replace): Replace cannot be "" in this function;
Substr_replace ($Str, $rep, $start [, Length]): $str The original string, $rep the replacement of the new string, $start the start, $length the length of the replacement, the optional

4, character length
int strlen ($STR)

5. Comparing character functions
int strcmp ($str 1, $str 2): $str 1>=< $str 2 is positive 1,0,-1 (string comparison)
STRCASECMP () ditto (case-insensitive)
STRNATCMP ("4", "14") compare strings by natural sort
STRNATCASECMP () ditto, (case-sensitive)

6, split the array of PHP string functions
Str_split ($str, Len): Dividing the $str by Len length to return an array
Split (search, $str [, int]): Divide $str by search character return array int is split several times, the following will not split expload (Search, $str [, int])

7, remove the space:
LTrim (), RTrim (), Trim ()

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

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

10, HTML code related functions
NL2BR (): Convert \ n to <br>.
Strip_tags ($str [, ' <p> ']): Removing HTML and PHP tags

All HTML and PHP code in $STR will be removed, and optional parameters for HTML and PHP code will be reserved for the optional parameters written in the code.
such as: Echo Strip_tags ($text, ' <br><p> ');
Htmlspecialchars ($str [, Parameters]): page normal output HTML code parameters, is the conversion mode

11, character case conversion of PHP string function
Strtolower ($STR): Converting strings to lowercase
Strtoupper ($STR): Converting strings 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): Converts single quotes ('), double quotes ("), backslashes (\) and NUL
strings in str to \, \, \ \。
MAGIC_QUOTES_GPC = On: Automatically escapes the contents of Get,post,cookie
GET_MAGIC_QUOTES_GPC (): Detects whether the MAGIC_QUOTES_GPC is turned on
stripslashes (): Remove backslash in 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.