3v4l.org

run code in 300+ PHP versions simultaneously
<?php function setKeyWords($str,$word_arr=array()){ // 设置多字节字符内部编码为utf8 mb_internal_encoding("UTF-8"); // 创建一个跟字符串长度一致的数组,用0填充 $map=array_fill(0,mb_strlen($str)+1,0); // 遍历关键词数组,将关键词对应的map数组的位置上的数字置为1 foreach ($word_arr as $value) { $pos=-1; $pos_count=0; $pos_arr=array(); // 如果找到了这个关键词,就将这个词的位置存入位置数组中(来支持多次出现此关键词的情况) while(($pos=mb_strpos($str,$value,$pos+1))!==false){ $pos_arr[]=$pos; $pos_count++; }ti // 遍历数组,将对应位置置1 foreach ($pos_arr as $pos_val) { if($pos_val!==false){ $fill=array_fill($pos_val,mb_strlen($value),1); $map = array_replace($map,$fill); } } $pos=null; } // 遍历map数组,加入高亮代码 $flag=0; $position=-1; $result=""; // 结果数组 foreach ($map as $key => $value) { if($value==1){ // 如果第一次出现1,则加上html标签头 if($flag==0) $result.="<span class=\"fc-red\">"; $flag=1; }else{ // 如果已经到了一个0,但上一个还是1时,加入html标签尾 if($flag==1){ $position=$key-1; $flag=0; $result.="</span>"; } } // 将该位置的字符加入结果字符串中 $result.=mb_substr($str,$key,1); } return $result; } $title="服务员服务啦"; $arr=array("服务","服务员"); echo setKeyWords($title,$arr);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
5.4.320.0070.03612.52
5.4.310.0050.03812.51
5.4.300.0050.03612.52
5.4.290.0050.04412.51
5.4.280.0080.03512.41
5.4.270.0070.03712.41
5.4.260.0040.04012.41
5.4.250.0040.04012.41
5.4.240.0070.03512.41
5.4.230.0050.04112.40
5.4.220.0070.03612.40
5.4.210.0070.03512.40
5.4.200.0050.03812.40
5.4.190.0060.03912.40
5.4.180.0060.04312.39
5.4.170.0040.03712.40
5.4.160.0080.03612.40
5.4.150.0070.03612.39
5.4.140.0060.03912.09
5.4.130.0060.03412.07
5.4.120.0050.03812.03
5.4.110.0060.03412.03
5.4.100.0040.03812.03
5.4.90.0060.03612.03
5.4.80.0030.04612.03
5.4.70.0090.03912.03
5.4.60.0070.03512.03
5.4.50.0080.03512.03
5.4.40.0070.03412.01
5.4.30.0080.03812.01
5.4.20.0080.03512.01
5.4.10.0050.04012.01
5.4.00.0080.03811.51
5.3.290.0050.03812.80
5.3.280.0050.03712.71
5.3.270.0060.03812.72
5.3.260.0050.03912.72
5.3.250.0050.03712.72
5.3.240.0080.03512.72
5.3.230.0070.03712.71
5.3.220.0040.04212.68
5.3.210.0090.03812.68
5.3.200.0060.03612.68
5.3.190.0070.03812.68
5.3.180.0040.03912.67
5.3.170.0090.03812.67
5.3.160.0080.03612.67
5.3.150.0090.03612.67
5.3.140.0070.03612.66
5.3.130.0080.03612.66
5.3.120.0070.03912.66
5.3.110.0040.04112.66
5.3.100.0090.03712.13
5.3.90.0070.03612.11
5.3.80.0100.03412.09
5.3.70.0060.03812.10
5.3.60.0070.04112.09
5.3.50.0110.04112.03
5.3.40.0070.04112.03
5.3.30.0060.03411.98
5.3.20.0070.03911.78
5.3.10.0070.03411.74
5.3.00.0070.03511.72
5.2.170.0040.0329.23
5.2.160.0050.0279.22
5.2.150.0040.0349.22
5.2.140.0040.0319.22
5.2.130.0050.0299.18
5.2.120.0040.0329.18
5.2.110.0050.0309.19
5.2.100.0040.0309.18
5.2.90.0050.0299.18
5.2.80.0060.0329.18
5.2.70.0070.0299.18
5.2.60.0080.0299.13
5.2.50.0090.0299.10
5.2.40.0050.0329.08
5.2.30.0060.0319.05
5.2.20.0040.0299.04
5.2.10.0080.0278.95
5.2.00.0050.0358.81
5.1.60.0050.0338.09
5.1.50.0040.0258.10
5.1.40.0030.0258.07
5.1.30.0050.0258.43
5.1.20.0060.0258.45
5.1.10.0040.0298.17
5.1.00.0050.0258.17
5.0.50.0040.0196.64
5.0.40.0050.0186.51
5.0.30.0050.0306.32
5.0.20.0040.0196.29
5.0.10.0030.0216.26
5.0.00.0040.0306.26
4.4.90.0020.0194.78
4.4.80.0030.0164.75
4.4.70.0040.0184.76
4.4.60.0020.0204.75
4.4.50.0040.0164.77
4.4.40.0040.0264.71
4.4.30.0040.0144.76
4.4.20.0020.0164.84
4.4.10.0040.0164.85
4.4.00.0030.0254.76
4.3.110.0020.0174.67
4.3.100.0020.0154.66
4.3.90.0000.0184.63
4.3.80.0030.0244.58
4.3.70.0040.0144.63
4.3.60.0020.0164.63
4.3.50.0030.0164.63
4.3.40.0020.0264.54
4.3.30.0030.0183.30
4.3.20.0020.0203.28
4.3.10.0020.0233.24
4.3.00.0030.0207.29

preferences:
146.02 ms | 1394 KiB | 7 Q