Discussion on the summary and analysis of various PHP string Functions _php Tutorial

Source: Internet
Author: User
Tags php language knowledge base
PHP String functions include finding character position functions, extracting sub-character functions, replacing strings, character lengths, comparing character functions, dividing the array of characters, removing spaces, and so on.

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 the character position function:
Strpos ($str, Search,[int]): Find the first position of search in $str starting with int;
Stripos ($str, Search,[int]):The 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]):Ditto, this function is not case sensitive

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 replaced new string, $start the start, $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 2 are positive 1,0,-1 (string comparison)
strcasecmp () ditto (no case)
strnatcmp ("4", " +") 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 return array int is split several times, followed by the 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 \ 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 retain the code written by the optional parameters.
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

http://www.bkjia.com/PHPjc/327300.html www.bkjia.com true http://www.bkjia.com/PHPjc/327300.html techarticle PHP String functions include finding character position functions, extracting sub-character functions, replacing strings, character lengths, comparing character functions, dividing the array of characters, removing spaces, and so on. PHP language ...

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