dpwtr666638 2017-08-06 03:01
浏览 37
已采纳

仅输出具有重复单词的行

I'm trying to take a list of lines, and have PHP only output the lines that contain the same word (variable) twice. It should match both singular and plural versions of the word.

Example list of lines:

This is a best website of all the websites out there

This is a great website

Here is a website I found while looking for websites

Website is a cool new word

I would put these lines into a textbox, and the script would output:

This is a best website of all the websites out there

Here is a website I found while looking for websites


No need for displaying any counts, only the raw lines that include the word twice.

I'm pretty decent at manipulating lines, but I searched everywhere for the answer to this, it seems to not exist.

  • 写回答

1条回答 默认 最新

  • dongyun6229 2017-08-06 04:16
    关注

    For the test purpose I didn't use something like $text = $_POST['text'];, instead I used a variable to store the text, Also the class I'm using to pluralize words comes from here.

    Note: I rolled back the answer to address exactly the question, the previous answer which was trying to address the comments has been moved here.

    <?php    
    
    $text = "This is a best website of all the websites out there
        This is a great website
        Here is a website I found while looking for websites
        Website is a cool new word';
    
    // helps us pluralize all words, so we can check the duplicates 
    include('class.php'); 
    
    // loop into each line one by one
    foreach(explode("
    ", $text) as $line)
    {
            // remove special characters
            $tline = preg_replace('/[^A-Za-z0-9\-\s]/', '', $line);
    
            // create a list of words from current line
            $words_list = preg_split('/\s+/', strtolower($tline));
    
            // convert all singular words to plural
            foreach($words_list as $word)
            {
                    $w[] = Inflect::pluralize($word);
            }
    
             // if the count of words in this line was bigger that of unique
             // words then we got some duplicates, echo this line out
            if( count($w) > count(array_unique($w)) )
                    echo $line . '</br>';
    
            // empty the array for next line
            $w = [];
    }
    

    The output for your desired text would be:

    This is a best website of all the websites out there
    Here is a website I found while looking for websites
    

    However the correctness of code really depends on how our pluralize method is working.


    How it's working

    First I'm looping into each line one by one using, at each iteration I'm making a list of words from that line with, then we should convert all singular words to plurals (or plural to singular it doesn't really matters), Now I've got a list of words which all of them are plural and I can easily check them to see if all of them are unique or not, if the number of words on that line is bigger than of the unique words then I can find out there are duplicates word there so I should print that line out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线