PHP commonly used string processing functions:

Source: Internet
Author: User


1 Find the character position function:
Strpos ($str, $seach, [int]); Find the first position of search in $str starting with int;
Stripos ($str, $seach, [int]); The function returns the position of the first occurrence of the 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]): Ibid., this function is insensitive to case


2. Functions to extract substrings
substr ($str, int start,[int len])
strstr ($str 1,str2)
stristr () function with strstr, just case insensitive
STRRCHR () prompt returned from the last word searched; use: Fetch the file name in the path


3. PHP string function for replacing strings
Str_replace (search,replace,source) Find search Replace with Replace in source
str_ireplace (Search,replace, $str): ibid., this function is insensitive to case
strtr ($str, $from, $to) The function returns a copy of Str and converts the character specified in the From to the corresponding character in to
<?php
$trans = Array ("Hello" = "hi", "hi" = "Hello");
Echo strtr ("Hi all, I said hello", $trans);
?>
substr_replace ($Str, $rep, $start [, Length]): $str The original string, $rep the new string after replacement, $start the starting 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>=< $str 2 are positive 1,0,-1 (string comparison)
strcasecmp () ignoring case (IBID.)
strnatcmp ("8", "18") compare strings by natural sort
strnatcasecmp ()


6. PHP String functions for splitting into groups
Str_split ($str, Len)
Split (search, $STR, [int])
explode (search, $str, [int]) splits the $STR by the search character return array int is split several times, the back will not be split


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


8, add the 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 () make \ n convert to <br>
strip_tags ($str, [' <p> ']) remove HTML and PHP tags
such as: Echo Strip_tags ($text, ' <br><p> ')
Htmlspecialchars ($str [, var])


11. Character-case-conversion PHP String Functions
strolower (str)
stroupper (str)
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 the single quotation mark ('), double quotation mark ("), backslash (\), and nul string in str 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






PHP commonly used string processing 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.