1

I am having the following example string:

$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.";

What I need is a way to add two br's (line break) each time when three dots were in the string while making sure it does not add the br's if the word before the dot is less than 4 characters so it does not split things like Mr. Mrs. Dr.

The result I am looking for based on the string above would be:

    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.

So what I am basically looking for is a function like:

$string = SplitString($string);

which would take care of the format that I am looking for.

UPDATE:

The posted solution works fine, except that domain names are split also.

Example string where it shows the issue:

    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.

The snippet splits it like this:

    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.

Besides that, it seems to work perfect! Maybe the trick is somehow to check if there is a space after the dot to prevent domains from being split?

4

3 回答 3

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.
于 2013-08-08T14:58:07.530 回答
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.

它检测以下单词是否符合您的条件:

  • 沃尔特
  • 文本
  • 昨天
  • 你好
  • 那里
  • 孩子们
于 2013-08-08T15:46:22.933 回答
-2

尝试这个:

$string =<<< Mr.Bean 说:一定很棒……懂PHP。他补充说,IP 1.2.3.4 是人们可以拥有的最佳 IP 地址。沃尔特夫人。今天是美好的一天。明天,会下雨。这实际上只是一些随机文本。你听到这只唱歌的鸟了吗?我昨天访问了柏林。... 正在下雨,憨豆先生打招呼。比恩夫人也在那儿…… 他们有两个孩子。 ;EOD
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

于 2013-08-08T15:29:56.697 回答