Demo PHP substr function using _php tutorial

Source: Internet
Author: User
PHP is our most commonly used dynamic development Web page language, string processing in programming is the longest use, we are here to discuss the use of PHP substr in detail. There is a bunch of strings similar to the following variable $abc, the end of which is more than one, (comma), I want to remove the end of the comma, in order to verify the execution time of different processing methods, the following is a comparison program I wrote to test:

program One, PHP substr

 
 
  1. php
  2. function Getmicrotime () {
  3. List ($usec, $sec) = Explode ("", Microtime ());
  4. return (float) $usec + (float) $sec);
  5. }
  6. $ Time_start = Getmicrotime ();
  7. $ ABC = "Chongqing, Wanxian, Fuling, Yuzhong, Dadukou, Jiangbei, Shapingba, Jiulongpo, Southbank, Beibei, Wansheng, Shuang Cambridge, Yubei, Banan, Qianjiang, longevity, Qijiang, Tongnan, Tongliang,
    Dazu, Rongchang, Bishan, liangping, Chengkou, Fengdou, Dianjiang, Wulong, none, Kai, Yunyang, Fengjie, Wushan, Wuxi, Pillar, ";
  8. $ L = strlen ($ABC);
  9. $ ABC = substr ($abc, 0, $l-1);
  10. echo $abc. " <br> ";
  11. $ Time_end = Getmicrotime ();
  12. Echo ($time _end-$time _start) *1000;
  13. ?>

Program two, using regular expressions

 
 
  1. PHP
  2. function Getmicrotime () {
  3. List ($usec, $sec) = Explode ("", Microtime ());
  4. return (float) $usec + (float) $sec);
  5. }
  6. $ Time_start = Getmicrotime ();
  7. $ ABC = "Chongqing, Wanxian, Fuling, Yuzhong, Dadukou, Jiangbei, Shapingba, Jiulongpo, Southbank, Beibei, Wansheng, Shuang Cambridge, Yubei, Banan, Qianjiang, longevity, Qijiang, Tongnan, Tongliang,
    Dazu, Rongchang, Bishan, liangping, Chengkou, Fengdou, Dianjiang, Wulong, none, Kai, Yunyang, Fengjie, Wushan, Wuxi, Pillar, ";
  8. $ ABC = Str_replace (", $", "", $ABC);
  9. echo $abc. " < BR > ";
  10. $ Time_end = Getmicrotime ();
  11. Echo ($time _end-$time _start) *1000;
  12. ?>

Results The average execution time of program one is 0.045, and the average execution time of program two is 0.085. The average execution time without any processing is 0.041. It can be seen that the first method of processing efficiency is 50% higher. Thus, in the process of doing the procedures, especially in the requirements of efficient procedures, we should compare the different methods to find the most optimized processing methods, the above is the PHP substr and regular expression program examples.


http://www.bkjia.com/PHPjc/446480.html www.bkjia.com true http://www.bkjia.com/PHPjc/446480.html techarticle PHP is our most commonly used dynamic development Web page language, string processing in programming is the longest use, we are here to discuss the use of PHP substr in detail. Have a bunch of similar to ...

  • Related Article

    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.