如何根据该句子中的字符串匹配从段落中删除特定句子?

时间:2012-12-14 07:27:43

标签: php string

例如我有以下段落

  

当您准备完成采访时,请单击“采访我”。别担心,在您接受完整的面试之前,您将有机会练习。截止日期为2030年12月30日,所以请确保您在此之前回复我们。感谢您的申请。

我需要删除包含'2030年12月30日'的整个句子。

请提出解决方案。

3 个答案:

答案 0 :(得分:2)

您可以通过多种方式执行此操作,但我会使用此方法:

  1. .
  2. 上展开您的字符串
  3. 浏览您的值并仅保留December 30, 2030不存在的值。
  4. 但如果你在句子里面有.,那么这会有问题。在这种情况下,您需要考虑一种不同的方式来将句子彼此分开。

    $string = "When you are ready to complete the interview, click Interview me . Don't worry, you will have a chance to practice before you take the full interview. The job closing date is December 30, 2030 so make sure you have your response back to us by then. Thanks for applying.";
    $search = "December 30, 2030";
    
    $sentences = explode( '.', $string );
    
    $new_string = '';
    foreach ( $sentences as $sentece )
    {
        if ( !strpos( $sentece, $search ) )
            $new_string .= $sentece . '.';
    }
    

    输出:

    When you are ready to complete the interview, click Interview me . Don't worry, you will have a chance to practice before you take the full interview. Thanks for applying.
    

答案 1 :(得分:1)

为什么不先把它分成句子?然后针对每个句子运行测试,如果测试告诉您应该包含它,则只将其包含在输出中?

答案 2 :(得分:-1)

一个简单的解决方案是替代

str_replace('December 30, 2030', '', 'When you are ready to complete the interview, click Interview me . Don't worry, you will have a chance to practice before you take the full interview. The job closing date is December 30, 2030 so make sure you have your response back to us by then. Thanks for applying');

这样您也可以使用当前日期更改它:

str_replace('December 30, 2030', date('l jS \of F Y h:i:s A'), 'When you are ready to complete the interview, click Interview me . Don't worry, you will have a chance to practice before you take the full interview. The job closing date is December 30, 2030 so make sure you have your response back to us by then. Thanks for applying');
相关问题