(Basic) PHP string functions

Source: Internet
Author: User
PHP string functions include the character location search function, the sub-character extraction function, the replacement string, the character length, the comparison character function, the array character, and the space. PHP string functions include the character location search function, the sub-character extraction function, the replacement string, the character length, the comparison character function, the array character, and the space.

String functions in PHP are also easy to understand. Today, we have summarized nearly 12 PHP string functions for you, hoping to help other users and increase their PHP knowledge base.

1. character location search function:
Strpos ($ str, search, [int]): searches for the first position of search in $ str starting from int;
Stripos ($ str, search, [int]): the position where the string first appears in another string. This function is case-insensitive. strrpos ($ str, search, [int]): searches for the last occurrence position of search in $ str starting from int.
Strripos ($ str, search, [int]): Same as above. This function is case insensitive.


2. extract the sub-string function (dual-byte)
Substr ($ str, int start [, int length]): Extracts [length strings] starting from the strat position in $ str.
Strstr ($ str1, $ str2): Searches for $ str2 from $ str1 (the first position) and truncates the string from the start to the end. if not, FALSE is returned.
Stristr () is the same as strstr, but is case insensitive.
Strrchr () is returned from the last searched character. usage: get the file name in the path


3. PHP string functions that replace strings
Str_replace (search, replace, $ str): search for search from $ str and replace it with replace.
Str_ireplace (search, replace, $ str): Same as above. This function is case insensitive.
Strtr ($ str, search, replace): In this function, replace cannot be "";
Substr_replace ($ Str, $ rep, $ start [, length]): $ str original string, $ new string after rep replacement, $ start, $ length, optional


4. character length
Int strlen ($ str)


5. Comparison of character functions
Int strcmp ($ str1, $ str2): $ str1 >=< $ str2 is positive 1, 0,-1 (string comparison)
Strcasecmp () Same as above (case-insensitive)
Strnatcmp ("4", "14") compares strings in natural order
Strnatcasecmp () is the same as above (case sensitive)


6. PHP string functions split into arrays
Str_split ($ str, len): Splits $ str by len length and returns an array.
Split (search, $ str [, int]): Splits $ str into search characters and returns the number of times that the array int is split, $ str [, int])


7. remove spaces:
Ltrim (), rtrim (), trim ()


8. space function
Chunk_split ($ str, 2): add a space to the $ str string;


9. chr and ord -- return the specified character or ascii

10. functions related to HTML code
Nl2br (): converts \ n
.
Strip_tags ($ str [,'

']): Removes HTML and PHP tags.

In $ str, all HTML and PHP code will be removed. the optional parameters are html and PHP code, which will retain the code written by the optional parameters.
For example, echo strip_tags ($ text ,'

');
Htmlspecialchars ($ str [, parameter]): HTML code parameters are output normally on the page, which is a conversion method.


11. PHP string functions for case-insensitive character conversion
Strtolower ($ str): converts a string to lowercase.
Strtoupper ($ str): converts string 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): Enables single quotation marks ('), double quotation marks ("), backslash (\), and NUL in str.
String \',\",\\.
Magic_quotes_gpc = On: automatically escapes get, post, and cookie content.
Get_magic_quotes_gpc (): checks whether magic_quotes_gpc is enabled.
Stripslashes (): removes the backslash from the string.

The above is the content of the PHP string function (basic). For more information, see The PHP Chinese website (www.php1.cn )!

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.