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);
Output for 5.4.0 - 5.4.32
Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in /in/pAtmA on line 18
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_FOREACH in /in/pAtmA on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FOREACH in /in/pAtmA on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/pAtmA on line 18
Process exited with code 255.

preferences:
223.7 ms | 1394 KiB | 123 Q