duanbarong4617 2014-04-09 11:59
浏览 20
已采纳

有人可以解释下面给出的代码吗?

$array_words is an array which has every 'single' word from a file in it.


$array_2_words = implode(" ", $array_words);

$array_2_words = preg_replace("/[^A-Z0-9-\' ]/i", "", $array_2_words);

$array_2_words = str_replace("  ","",$array_2_words);

$jk = explode(" ",$array_2_words);

$i = sizeof($jk);
$j = 0;

$array_2_words="";

for($j=0;$j<$i-1;$j++)
{
    $array_2_words[] = $jk[$j]." ".$jk[$j+1];
}

I know what the result of this code is, making an array with 2 words each. For example: It will make from a sentence like this: This is a test sentence the following array:

This is is a a test test sentence

But would someone be so kind to comment (each|some) line(s) to how it exactly does this from an array with 1 word in every entry to 2 words like my example.

I could just leave this code in my script but I will only fool myself by not knowing how it works.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douguazhi5966 2014-04-09 12:07
    关注

    The magic happens only on the last line.

    $array_2_words[] = $jk[$j]." ".$jk[$j+1];
    

    This line means: Take an array of words ($jk) and combine word n and word n+1 from the array of words and push them into $array_2_words.

    so what happens is:

    $jk[0]." ".$jk[1] = "This" + " " + "is" = "This is"
    $jk[1]." ".$jk[2] = "is" + " " + "a" = "is a"
    $jk[2]." ".$jk[3] = "a" + " " + "test" = "a test"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题