PHP Introductory tutorials on string handling techniques summary (conversion, filtering, parsing, searching, interception, substitution, etc.) _php tips

Source: Internet
Author: User

The examples in this article summarize the techniques of string processing in PHP. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  //source code is text form, page display is Web Form
  $str = '      php      ';
  Clear the two sides of the space LTrim only clean left, RTrim only clear the right
  Echo LTrim ($STR);
  Echo ' <br/> ';
  echo RTrim ($STR);
  Echo ' <br/> ';
  Both sides clean the
  echo trim ($STR);
  Echo Chop ($STR);
? >

demo2.php

<?php
  $str = "This is a one-stop build site \ n is a onestopweb";
  However, I now want to implement a newline in the Web page
  //In the reply, a carriage return is \ n
  //We use the function to implement the conversion process
  echo nl2br ($STR);
? >

demo3.php

<?php
  //Convert all characters to HTML
  $str = ' <strong> read who asked you to chant, water fall fragrance floating. </strong> ';
  echo htmlentities ($STR);
  We only need to convert special characters can
  //echo htmlspecialchars ($str);//<strong> read who asked you to chant, water fall fragrance floating. </strong>
  Echo strip_tags ($STR);//See who asked you to chant, water fall fragrance floating.
?>

demo4.php

<?php
  $str = ' This is ' one-stop building site \ n ' is a onestopweb ';
  For the string that will be inserted into the database, handle the problematic characters
  //echo addslashes ($STR);//this is \ "One-stop build site \" \\n This is a onestopweb
  $a = Addslashe S ($str);
  This $a is written to the database, I take out the words, there will be \ This symbol
  echo stripcslashes ($a);
  First, the string written into the database is filtered by the addslashes () function, and then the
  stripcslashes () is parsed to show the
?>

demo5.php

<?php
  //Converts the string to uppercase
  echo strtoupper (' Onestopweb ');//onestopweb
  //Converts the string to lowercase
  echo strtolower ( ' Onestopweb ');//onestopweb
  //Convert the first letter to uppercase
  Echo ucfirst (' Onestopweb ');//onestopweb
?>

demo6.php

<?php
  $str = ' onestopweb ';
  Echo Str_pad ($STR, 11). ' is good! '; Onestopweb is good!
  onestopweb###
  //echo Str_pad ($str,, ' # ')
  echo str_pad ($STR, N, ' # ', str_pad_both);
  Echo ' <br/> ';
  Echo Str_pad ($STR, N, ' # ', str_pad_left);
  Echo ' <br/> ';
  Echo Str_pad ($STR, N, ' # ', str_pad_right);
/**
 * ### #oneStopWeb *
 ####### #oneStopWeb
 * onestopweb########
 *
* * * * * *?>

demo7.php

<?php
  //explode--using one string to split another string
  //Return is an array
  //explode the first parameter is a split string, the second parameter is a string to be split
  $email = Explode (' @ ', ' oneStopWeb@163.com ');
  Print_r ($email); Array ([0] => onestopweb [1] => 163.com)
  //After split, I pass a round of filtering and regroup
//$arr = Array (' OneStop ', ' @ ', ' 163.c Om ');
$str = Implode (' & ', $arr);
echo $str//onestop&@&163.com
  $str = implode ('-', $email);
  Echo $str; Onestopweb-163.com
?>

demo8.php

<?php
//$str = ' I'll be the back ';
$arr = Explode (", $str);
Print_r ($arr); Array ([0] => I [1] => would [2] => is [3] => back)
  $str = ' i,will.be#back ';
  $tok = Strtok ($str, ',. # ');
  echo $tok;
  while ($tok) {
    echo $tok. ' <br/> ';
    $tok = Strtok (',. # ');
  }
$str = ' I'll be the back ';
$tok = Strtok ($str, ");
echo $tok;
while ($tok) {
//   echo $tok. ' <br/> ';   $tok = Strtok (");
}
//$tok = Strtok (");
echo $tok//iwill////
pointer not moved down, but again
//$tok = Strtok ($str, ");
echo $tok;
? >

demo9.php

<?php
  $str = ' oneStopWeb@163.com ';
  The middle parameter represents the starting position, the position is starting at 0, and the last parameter is the number of
  substr Echo ($str, 0,5)  ;//onest
?>

demo10.php

<?php
  //$str = ' oneStopWeb@163.com ';
  $str = ' Read who asked you to chant, water fall fragrance floating. '; Chinese garbled
  Print_r (Str_split ($STR));
? >

demo11.php

<?php
  $str = ' oneStopWeb@163.com ';
  echo Strrev ($STR); Moc.361@bewpotseno
?>

demo12.php

<?php/
  /through = = 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 ' equals ';
}
  //echo strcasecmp (' B ', ' B ');//No difference case
  //current is unnatural sort
  //echo strcmp (' 2 ', ' ten ');//1
  // What if you compare it by natural sorting?
  Echo strnatcmp (' 2 ', ' ten ');//-1
?>

demo13.php

<?php
  //strspn
  //The back two number of parameters, starting from the first few, take the number of
  echo strspn (' One ', ' oneStopWeb@163.com ', 1,5);
? >

demo14.php

<?php
  //test string length
  echo strlen (' oneStopWeb@163.com ');
? >

demo15.php

<?php
  //test string appears frequency
  echo substr_count (' oneStopWeb@163.com ', ' o ');
? >

demo16.php

<?php
  //The string after the output from the specified string
  echo strstr (' oneStopWeb@163.com ', ' @ ');
  echo stristr (' oneStopWeb@163.com ', ' s ') without distinction of case;
? >

demo17.php

<?php
  //Find the first occurrence of a string position
  /position is calculated from the No. 0 position, W first appears in the 7th position of
  Echo strpos (' oneStopWeb@163.com ', ' W ');
  The last occurrence of the position
  echo strrpos (' oneStopWeb@163.com ', ' o ');
? >

demo18.php

<?php
  //String substitution
  ///The first parameter represents the string to find (part)
  ///The second parameter represents the string to be replaced (part)
  ///The third parameter represents the original replaced full string
  //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
  //starts at the first position (parameter 3), takes out 5 (parameter 4), replaces it with ### (parameter 2)
  echo substr_replace (' oneStopWeb@163.com ', ' ### ', 0,5);
? >

demo19.php

<?php
  //Take Chinese length
  $str = ' Read who asked you, the water fell fragrance floating. ';
  Use ordinary strlen to take a Chinese character, even if two
  //echo strlen ($STR);
  Using Mb_strlen to get Chinese, there is a second parameter, the character encoding
  Echo Mb_strlen ($str, ' GBK ');//12
  //If you use the normal strlen this function, I take 1 characters
  //MB _substr to take the Chinese character
  Echo mb_substr ($str, 2,1, ' GBK ');/ask
?>

demo20.php

<?php
  $str = ' Read who asked you to chant, water fall fragrance floating. ';
  Find o first occurrence of the position
  //Chinese algorithm, two characters a Chinese character
  //mb even if a
  //echo strpos ($str, ' ask ');//4
  //According to one character in Chinese, this will not appear half the Chinese state
  Echo mb_strpos ($str, ' Q ', 0, ' GBK ');
? >

demo21.php

<?php
//$str = ' Read who asked you to chant, water fall fragrance floating. '  ;
Echo mb_substr ($str, 0,1, ' GBK ');
  $str = ' Read who asked you to chant, water fall fragrance floating. ';
  This does not matter with Mb_strstr
  Echo strstr ($str, ' water ');
>

demo22.php

<?php
  $str = ' Read who asked you to chant, water fall fragrance floating. ';
  Echo Mb_substr_count ($str, ' ask ', ' GBK ');
? >

More interested in PHP related content readers can view the site topics: "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.