如果空格前的单词超过3个字符,如何在三个句点后添加

时间:2013-08-08 14:52:17

标签: php

我有以下示例字符串:

$string = "Mr.Bean said: It must be great to know PHP. 
He added that IP 1.2.3.4 is the best IP address one could own. 
Mrs. Walter. Today is a wonderful day. Tomorrow, it will rain. 
This is really just some random text. Did you hear this singing bird? 
I visited Berlin yesterday. It was raining and Mr. Bean said hello. 
Mrs. Bean was also there. They have two kids.";

我需要的是每次在字符串中有三个点时添加两个br(换行符)的方法,同时确保如果点之前的单词小于4个字符则不添加br,因此它不会像Mrs. Mrs. Dr.博士那样分开了。

我根据上面的字符串查找的结果是:

    Mr.Bean said: It must be great to know PHP. He added that IP 1.2.3.4 is the best IP address one could own. Mrs. Walter. 

    Today is a wonderful day. Tomorrow, it will rain. This is really just some random text. 

    Did you hear this singing bird? I visited Berlin yesterday. It was raining and Mr. Bean said hello. Mrs. Bean was also there. 

    They have two kids.

所以我基本上寻找的是一个像以下的函数:

$string = SplitString($string);

将处理我正在寻找的格式。

更新:

发布的解决方案正常,但域名也会被拆分。

显示问题的示例字符串:

    Bill Price reveals his new method for playing the diatonic harmonica, whereby anyone can convert their ordinary ten-hole diatonic harmonicas into lead instruments. “Coupling is a patented method; guaranteed to expand the harmonica's range and over-all capability.” The major key diatonic coupling formula for this method is disclosed at: http://example.com/. You’ll enjoy samples from Bill’s unique ten-song CD, “Taking the Lead”. This CD displays dynamic possibilities available when pairing/coupling two diatonic harmonicas together. The result is truly astonishing! The formula is free for visiting example.com. The CD is only $8, plus it can be used as a tutorial for learning the Coupling Method.

该片段将其拆分为:

    Bill Price reveals his new method for playing the diatonic harmonica, whereby anyone can convert their ordinary ten-hole diatonic harmonicas into lead instruments. “Coupling is a patented method; guaranteed to expand the harmonica's range and over-all capability.” The major key diatonic coupling formula for this method is disclosed at: http://example.

    com/. You’ll enjoy samples from Bill’s unique ten-song CD, “Taking the Lead”. This CD displays dynamic possibilities available when pairing/coupling two diatonic harmonicas together.

    The result is truly astonishing! The formula is free for visiting example.com. The CD is only $8, plus it can be used as a tutorial for learning the Coupling Method.

除此之外,它似乎完美无缺!也许诀窍是以某种方式检查点后面是否有空格以防止域被分割?

3 个答案:

答案 0 :(得分:4)

$string = "Mr.Bean said: It must be great to know PHP. He added that IP 1.2.3.4 is the best IP address one could own. Mrs. Walter. Today is a wonderful day. Tomorrow, it will rain. This is really just some random text. Did you hear this singing bird? I visited Berlin yesterday. It was raining and Mr. Bean said hello. Mrs. Bean was also there. They have two kids.";
function splitString($str)
{
    //look for 2 full stops, then another full stop with at least 4 word characters in front of it
    preg_match_all('#(.+?\..+?\..+?\w{4,}\.)#',$str,$matches);
    return implode('<br><br>',array_map('trim',$matches[1]));
}
echo splitString($string);

输出:

Mr.Bean said: It must be great to know PHP. He added that IP 1.2.3.4 is the best IP address one could own. Mrs. Walter.

Today is a wonderful day. Tomorrow, it will rain. This is really just some random text.

Did you hear this singing bird? I visited Berlin yesterday. It was raining and Mr. Bean said hello.

Mrs. Bean was also there. They have two kids.

新版本确保完全停止后不会立即显示任何内容:

$string = " Bill Price reveals his new method for playing the diatonic harmonica, whereby anyone can convert their ordinary ten-hole diatonic harmonicas into lead instruments. Coupling is a patented method; guaranteed to expand the harmonica's range and over-all capability. The major key diatonic coupling formula for this method is disclosed at: http://example.com/. You'll enjoy samples from Bill's unique ten-song CD, Taking the Lead. This CD displays dynamic possibilities available when pairing/coupling two diatonic harmonicas together. The result is truly astonishing! The formula is free for visiting example.com. The CD is only $8, plus it can be used as a tutorial for learning the Coupling Method.";
function splitString($str)
{
    preg_match_all('#(.+?\..+?\..+?\w{4,}\.($| ))#',$str,$matches);
    return implode('<br><br>',array_map('trim',$matches[1]));
}
echo splitString($string);

输出:

Bill Price reveals his new method for playing the diatonic harmonica, whereby anyone can convert their ordinary ten-hole diatonic harmonicas into lead instruments. Coupling is a patented method; guaranteed to expand the harmonica's range and over-all capability. The major key diatonic coupling formula for this method is disclosed at: http://example.com/. You'll enjoy samples from Bill's unique ten-song CD, Taking the Lead.

This CD displays dynamic possibilities available when pairing/coupling two diatonic harmonicas together. The result is truly astonishing! The formula is free for visiting example.com. The CD is only $8, plus it can be used as a tutorial for learning the Coupling Method.

答案 1 :(得分:0)

这样做你想要的但是输出与你期望的不同(看看ma,没有正则表达式):

function splitString($string) {
    $pieces = explode(".", $string); // Split string into array at each "."
    $count = count($pieces);
    $tally = 0;
    for ($i = 0; $i < $count - 1; $i++) { // Loop through each piece ignoring the last as it doesn't make sense to put a "<br />" after it.
       $piece = $pieces[$i];
       $words = explode(" ", $piece);
       if (strlen(trim($words[count($words)-1])) > 3) { // Check whether the last word in this piece is greater than 3 characters long
          $tally++;
          if ($tally % 3 == 0) { 
            $pieces[$i + 1] = '<br />' . ltrim($pieces[$i + 1]); // Prepend "<br />" to next piece and remove any preceding whitespace
          }
       }
       $pieces[$i] .= ".";
    }
    return implode($pieces); // Put pieces into a string
}

<强>输出

Mr.Bean said: It must be great to know PHP. He added that IP 1.2.3.4 is the best IP address one could own. Mrs. Walter. Today is a wonderful day. Tomorrow, it will rain. This is really just some random text.<br>Did you hear this singing bird? I visited Berlin yesterday. It was raining and Mr. Bean said hello. Mrs. Bean was also there.<br>They have two kids.

它会检测以下字词以符合您的条件:

  • 沃尔特
  • 文本
  • 昨天
  • 你好
  • 孩子

答案 2 :(得分:-2)

试试这个:

$ string =&lt;&lt;&lt; EOD
Mr.Bean说:知道PHP一定很棒。他补充说IP 1.2.3.4是最好的IP地址。沃尔特夫人今天是美好的一天。明天会下雨。这实际上只是一些随机文本。你听到这只唱歌的鸟了吗?我昨天去了柏林。 ......下雨了,憨豆先生打招呼。憨豆太太也在那里......他们有两个孩子。 EOD;

echo str_replace(“...”,“
”,$ string);

如果您想了解更多: http://php.net/manual/en/function.str-replace.php

为了获得最佳实践,请尝试使用Heredoc语法声明变量($ string): http://php.net/manual/en/language.types.string.php