Introduction to PHP string interception and interception functions

不言
Release: 2023-04-03 08:56:02
Original
7104 people have browsed it

This article introduces you to PHP string interception and string interception functions. Friends in need can refer to it.

1. Intercept according to the index where the characters are located:

1 $str = 'hello word,my name is zym'; 2 echo substr($str,11);//my name is zym 3 echo substr($str,11,2);//my
Copy after login

2. Intercept according to the specified characters:

01 $str = 'hello world,my name is zym'; 02 //截取某个字符在字符串中首次出现直到最后的所有字符(从左到右) 03 echo strchr($str,'my');//my name is zym 04 //另外一种写法 05 echo strstr($str,'my');//my name is zym 06 //不区分大小写的写法 07 echo stristr($str,'MY');//my name is zym 08 //截取某个字符在字符串中最后出现到最后的所有字符(从右到左) 09 echo strrchr($str,'o');//orld,my name is zym 10 //输出某个字符在字符串中首次出现的位置索引 11 echo strpos($str,'my');//12 12 //将字符串拆分成数组 13 $arry = explode(',',$str); 14 var_dump($arry);//array(2) { [0]=> string(11) "hello world" [1]=> string(14) "my name is zym" }
Copy after login

Do As an actual case, in a sentence, find out how many times a certain word appears and where it appears?

1 $str = 'hello world,my name is zym'; 2 $num=0; 3 echo '字母【o】出现了位置索引是:'; 4 for($i=0; strpos($str,'o',$i)!=0; $i=strpos($str,'o',$i)+1){ 5 $num+=1; 6 echo strpos($str,'o',$i).'、';//4 7 7 } 8 echo '字母【o】总共出现了'.$num.'次';//2
Copy after login

3. Split the main string by specifying characters (string splitting):

string strtok ( string $str , string $token ) string strtok ( string $token ) strtok() splits the string str into several substrings, each substring is split by the characters in token. This means that if you have a string like "This is an example string", you can use space characters to break up the sentence into separate words.

Note that the string parameter is only used the first time the strtok function is called. Each subsequent call to strtok will only use the token argument because it remembers its position within the string. If you want to start splitting a new string again, you need to use string again to call the strtok function to complete the initialization work. Note that multiple characters can be used in the token parameter. The string will be split by any character in this parameter.

1 $a = 'hello,world,my,name,is,zym'; 2 $b = strtok($a,','); 3 while($b){ 4 echo $b.'
'; 5 $b = strtok(','); 6 }
Copy after login

#4. Parse the query characters into variables:

1 $url = 'http://www.zymseo.com?username=zym&sex=男'; 2 $msg = substr($url,(strpos($url,'?')+1)); 3 parse_str($msg); 4 echo $username; 5 echo $sex;
Copy after login

5. Split the string every n characters , and insert specific delimiters:

Print code help

1 $a = 'abcdefghijklmnopqrstuvwxyz'; 2 echo chunk_split($a,3,' | ');//abc | def | ghi | jkl | mno | pqr | stu | vwx | yz |
Copy after login

Related recommendations:

PHP How to get a string between 2 characters

The above is the detailed content of Introduction to PHP string interception and interception functions. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!