Introduction to PHP string interception and interception functions

Source: Internet
Author: User
Tags strtok
This article introduces you to the PHP string interception and string interception function, the need for friends can refer to.

1. Intercept according to the index of the character:

1 $str = ' Hello word,my name is Zym '; 2 echo substr ($STR, one);//my name is zym3 echo substr ($str, 11,2);//my

2. Intercept according to the specified characters:

$str = ' Hello world,my name is Zym '; 02//intercepts the first occurrence of a character in a string until the last of all characters (from left to right) in the Echo strchr ($str, ' my ');//my name is zym04//another The outer one is the echo strstr ($str, ' my ');//my name is zym06//case-insensitive notation in echo stristr ($str, ' my ');//my name is zym08//intercepts a character in the string last Appears to the last of all characters (right to left) of the Echo strrchr ($str, ' O '),//orld,my name is ZYM10//output a character in the string where the first occurrence of the index of the position is echo strpos ($str, ' my ');// 1212//Split the string into an array of $arry = Explode (', ', $str); Var_dump ($arry);//array (2) {[0]=> string (one) "Hello World" [1]=> St Ring (+) "My Name is Zym"}

To do a real case, in a statement, find out how many times a word appears, where it appears in each place?

1 $str = ' Hello world,my name is Zym '; 2 $num the =0;3 echo ' o ' has a position index: '; 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  }8 echo ' o ' Total appeared '. $num. ';//2

3. Split the main string (string split) by the specified character:

String Strtok (String $str, String $token) string Strtok (String $token) strtok () divides the string str into substrings, with each substring in token Character segmentation. This also means that if there is a string that is the "this was an example string", you can use the space character to divide the sentence into separate words.

Note Use the string parameter only the first time you call the Strtok function. Each time you call Strtok, you will only use the token parameter, because it will remember its position in the string. If you want to start splitting a new string again, you need to use string again to invoke the Strtok function to complete the initialization work. Note You can use more than one character in the token parameter. The string will be split by any one of the characters in the parameter.

1 $a = ' Hello,world,my,name,is,zym '; 2 $b = Strtok ($a, ', '), 3 while ($b) {4    echo $b. ' <br/> '; 5    $b = Strtok (', ') ; 6}

4. Parse the query character into the variable:

1 $url = ' http://www.zymseo.com?username=zym&sex= male '; 2 $msg = substr ($url, (Strpos ($url, '? ') +1); 3 parse_str ($MSG); 4 echo $username; 5 echo $sex;

5. Split the string every n characters and insert a specific delimiter:

Print Code Help

1 $a = ' abcdefghijklmnopqrstuvwxyz '; 2 echo chunk_split ($a, 3, ' | ');//ABC | def | Ghi | JKL | MnO | PQR | Stu | vwx | YZ |

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.