PHP getting started tutorials-Summary of string processing techniques (conversion, filtering, parsing, searching, intercepting, replacement, etc.), php getting started tutorials
This example summarizes the PHP string processing skills. We will share this with you for your reference. The details are as follows:
Demo1.php
<? Php // the source code is in the text format, and the page is displayed in the web form $ str = 'php'; // clear the spaces on both sides. ltrim only clears the left, rtrim only clears echo ltrim ($ str); echo '<br/>'; echo rtrim ($ str); echo '<br/> '; // clear echo trim ($ str) on both sides; // echo chop ($ str);?>
Demo2.php
<? Php $ str = "This is an all-in-one website creation \ n This is a OneStopWeb"; // However, I want to implement line breaks in the webpage. // when replying to a post, A carriage return is \ n // we use the function to implement the conversion process echo nl2br ($ str);?>
Demo3.php
<? Php // convert all characters into HTML $ str = '<strong> who asked Junyi. </Strong> '; // echo htmlentities ($ str); // you only need to convert special characters. // echo htmlspecialchars ($ str ); // <strong> who can read this question. </Strong> echo strip_tags ($ str); // who asked Junyi?
Demo4.php
<? Php $ str = 'this is "all-in-one website creation" \ n This is a onestopweb'; // for strings to be inserted into the database, process the problematic characters // echo addslashes ($ str ); // This is \ "All-in-One website creation \" \ n This is a OneStopWeb $ a = addslashes ($ str); // This $ a is written to the database. If I take it out, echo stripcslashes ($ a); // first, filter the string written into the database using the addslashes () function, and then use stripcslashes () what is displayed after parsing?>
Demo5.php
<? Php // convert the string to echo strtoupper ('onestopweb'); // oneStopWeb // convert the string to lower case echo strtolower ('onestopweb '); // onestopweb // convert the first letter into an echo ucfirst ('onestopweb'); // oneStopWeb?>
Demo6.php
<?php $str = 'oneStopWeb'; //echo str_pad($str,11).'is good!'; //oneStopWeb is good! //oneStopWeb### //echo str_pad($str,13,'#') echo str_pad($str,18,'#',STR_PAD_BOTH); echo '<br/>'; echo str_pad($str,18,'#',STR_PAD_LEFT); echo '<br/>'; echo str_pad($str,18,'#',STR_PAD_RIGHT);/** * ####oneStopWeb#### * ########oneStopWeb * oneStopWeb######## * */?>
Demo7.php
<? Php // explode -- use a string to split another string // an array is returned // The first parameter of explode is the split string, the second parameter is the string to be split $ email = explode ('@', 'onestopweb @ 163.com '); // print_r ($ email ); // Array ([0] => oneStopWeb [1] => 163.com) // after the split, I perform a round of filtering, also re-combine // $ arr = array ('onestop', '@', '2017. com '); // $ str = implode (' & ', $ arr); // echo $ str; // oneStop & @ & 163.com $ str = implode ('-', $ email); echo $ str; // oneStopWeb-163.com?>
Demo8.php
<? Php // $ str = 'I will be back'; // $ arr = explode ('', $ str); // print_r ($ arr ); // Array ([0] => I [1] => will [2] => be [3] => back) $ str = 'I, will. be # back'; $ tok = strtok ($ str ,',. # '); // echo $ tok; while ($ tok) {echo $ tok. '<br/>'; $ tok = strtok (',. # ');} // $ str =' I will be back'; // $ tok = strtok ($ str, ''); // echo $ tok; // while ($ tok) {// echo $ tok. '<br/>'; // $ tok = strtok (''); //} // $ tok = strtok (' '); // Echo $ tok; // Iwill // the pointer is not moved down, but is re-sent. // $ tok = strtok ($ str ,''); // echo $ tok;?>
Demo9.php
<? Php $ str = 'onestopweb @ 163.com '; // The parameter in the middle indicates the start position, which starts from 0. The last parameter, is the number of retrieved echo substr ($ str,); // oneSt?>
Demo10.php
<? Php // $ str = 'onestopweb @ 163.com '; $ str =' Who asked Jun Yi. '; // Chinese garbled characters print_r (str_split ($ str);?>
Demo11.php
<?php $str = 'oneStopWeb@163.com'; echo strrev($str); //moc.361@beWpotSeno?>
Demo12.php
<? Php // use = to compare whether the string is consistent. // The final return value is a Boolean value. // echo 'A' = 'a '; // compare two strings // echo strcmp ('A', 'B'); // if (strcmp ('A', 'A') = 0) {// echo 'equal '; //} // echo strcasecmp (' B ',' B '); // case-insensitive // It is not in natural order at present // echo strcmp ('2', '10'); // 1 // What if it is compared by natural sorting? Echo strnatcmp ('2', '10'); //-1?>
Demo13.php
<? Php // strspns // the parameter of the next two digits, which is the number of echo strspns ('one', 'onestopweb @ 163.com ',) starting from the nth digit?>
Demo14.php
<? Php // test the string length echo strlen ('onestopweb @ 163.com ');?>
Demo15.php
<? Php // frequency of test string occurrence echo substr_count ('onestopweb @ 163.com ', 'O');?>
Demo16.php
<? Php // The echo strstr string after output from the specified string ('onestopweb @ 163.com ','@'); // case-insensitive echo stristr ('onestopweb @ 163.com ', 's');?>
Demo17.php
<? Php // search for the first position where a string appears // The position is calculated starting from the first position. For the first time, W appears at the first position, echo strpos ('onestopweb @ 163.com ', 'W'); // The final position echo strrpos ('onestopweb @ 163.com ', 'O');?>
Demo18.php
<? Php // string replacement // The first parameter indicates the string to be searched (partial) // The second parameter indicates the string to be replaced (partial) // The third parameter indicates the original complete string to be replaced // echo str_replace ('one', 'yi', 'onestopweb @ 163.com '); // yiStopWeb@163.com // str_ireplace is case insensitive // echo str_ireplace ('one', 'yi', 'onestopweb @ 163.com '); // yiStopWeb@163.com // start from the first position (parameter 3), take 5 (parameter 4), replace it with ### (parameter 2) echo substr_replace ('onestopweb @ 163.com ',' ###',);?>
Demo19.php
<? Php // get the Chinese length $ str = 'Who asked you. '; // Use a normal strlen to get a Chinese text, even if two // echo strlen ($ str); // use mb_strlen to get Chinese, there is a second parameter, echo mb_strlen ($ str, 'gbk'); // 12 // if you use the normal strlen function, I will take 1 character // use mb_substr to take the Chinese character echo mb_substr ($ str, 'gbk'); // question?>
Demow.php
<? Php $ str = 'Who asked you. '; // The algorithm for finding the location where o first appears // Chinese. If there are two characters in a text, // mb, the algorithm returns a // echo strpos ($ str, 'q '); // 4 // It is calculated by a single Chinese character, so that the status of half a Chinese character does not appear echo mb_strpos ($ str, 'Q', 0, 'gbk');?>
Demo21.php
<? Php // $ str = 'Who asked you. '; // Echo mb_substr ($ str, 'gbk'); $ str =' Who asked Junyi. '; // It doesn't matter if you use mb_strstr echo strstr ($ str, 'water');?>
Demo22.php
<? Php $ str = 'Who asked you. '; Echo mb_substr_count ($ str, 'Q', 'gbk');?>