PHP String Functions (Basic)

Source: Internet
Author: User
Tags 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]): function returns the position of the first occurrence of a string in another string. The function is insensitive to case Strrpos ($STR, Search,[int]): Find the last occurrence of search in $str starting 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 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", "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 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 <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 retain the code written by the optional parameters.
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 (): Remove backslash from string

The above is (Basic) PHP string function content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.