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
- php
- function Getmicrotime () {
- List ($usec, $sec) = Explode ("", Microtime ());
- return (float) $usec + (float) $sec);
- }
- $ Time_start = Getmicrotime ();
- $ 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, ";
- $ L = strlen ($ABC);
- $ ABC = substr ($abc, 0, $l-1);
- echo $abc. " <br> ";
- $ Time_end = Getmicrotime ();
- Echo ($time _end-$time _start) *1000;
- ?>
Program two, using regular expressions
- PHP
- function Getmicrotime () {
- List ($usec, $sec) = Explode ("", Microtime ());
- return (float) $usec + (float) $sec);
- }
- $ Time_start = Getmicrotime ();
- $ 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, ";
- $ ABC = Str_replace (", $", "", $ABC);
- echo $abc. " < BR > ";
- $ Time_end = Getmicrotime ();
- Echo ($time _end-$time _start) *1000;
- ?>
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 ...