Summary of string manipulation functions in php

黄舟
Release: 2023-03-15 13:42:01
Original
1964 people have browsed it

The following editor will bring you a collection of string operation functions based on PHP. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Find character position function:


strpos($str,search,[int])://查找search在$str中的第一次位置从int开始;
strrpos($str,search,[int])://查找search在$str中的最后一次出现的位置从int开始
Copy after login

2. Extract sub-character function (double bytes)


submit($str,int start[,int length])://从$str中strat位置开始提取[length长度的字符串]。
strstr($str1,$str2)://从$str1(第一个的位置)搜索$str2并从它开始截取到结束字符串;若没有则返回FALSE。
stristr()//功能同strstr,只是不区分大小写。
strrchr()//从最后一次搜索到的字符处返回;用处:取路径中文件名
Copy after login

3. Replace string


str_replace(search,replace,$str):从$str中查找search用replace来替换
str_irreplace(search,replace,$str):
strtr($str,search,replace):这个函数中replace不能为"";
substr_replace($Str,$rep,$start[,length])$str原始字符串,$rep替换后的新字符串,$start起始位置,$length替换的长度,该项可选
Copy after login

4. Query string length


int strlen($str)
Copy after login

5. Comparison character function

int strcmp($str1,$str2):$str1>=<$str2 is positive 1 respectively ,0,-1 (String comparison strcasecmp() Same as above (case-insensitive) strnatcmp("4","14") Compare strings in natural order strnatcasecmp() Same as above, (case-sensitive)

6. Split into array function

str_split($str,len): Split $str according to the length of len and return the array split(search,$str[, int]): Split $str according to the search character to return the array int. How many times will it be split? The following will not be split explode(search,$str[,int])

##7, Remove spaces:

ltrim, rtrim, trim

##8, add spaces functionchunk_split ($str,2); Press 2 characters into the $str character to add a space;

9. Return the specified character or ascii chr、ord


10. HTML code related functionsnl2br(): Convert \n to < br>.strip_tags($str[,'

']): //Remove HTML and PHP tags htmlspecialchars($str[,parameter])://The normal output of the page is the conversion method of HTML code parameters

11. Character case conversion function strtolower($str) Convert the string to lowercase strtoupper($str) Convert the string to uppercase ucfirst($ str) Convert the first character of the function to uppercase ucwords($str) Convert the first letter of each word to uppercase

12. Database related functions addslashes($str): Convert single quotes ('), double quotes ("), backslashes (\) and NUL strings in str to \',\",\\. magic_quotes_gpc = On automatically escapes the content of the get post cookie get_magic_quotes_gpc() detects whether magic_quotes_gpcstripslashes() is turned on and removes backslashes in the string

13. Connection functionimplode(str,$arr) concatenates the string array into a string according to the specified characters; the implode() function has an alias function join

The above is the detailed content of Summary of string manipulation functions in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template