我有以下示例字符串:
$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,这样它就不会像先生夫人那样分裂
基于上面的字符串,我正在寻找的结果是:
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.
除此之外,它似乎工作得很完美!也许诀窍是检查点后面是否有空格,以防止域被拆分?
$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.
这可以完成您想要的操作,但输出与您期望的不同(Look ma,no regex):
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.
它检测到以下单词符合您的标准:
- Walter
- 雨
- 文本
- 昨天
- 你好
- 那里
- 孩子
试试这个:
$string=<lt<EOD
憨豆先生说:一定很棒。。。了解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