我有以下示例字符串:
$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(换行),同时确保不添加br's,如果点前的单词小于4个字符,那么它就不会分割像Dr. .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.除此之外,它似乎工作得很完美!也许诀窍是检查点后是否有空格,以防止域被分割?
发布于 2013-08-08 14:58:07
$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-08 15:46:22
这可以实现您想要的结果,但是输出与您所期望的不同(请看ma,没有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.
它检测到与您的标准匹配的下列单词:
发布于 2013-08-08 15:29:56
试试这个:
$string =<<<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
https://stackoverflow.com/questions/18129212
复制相似问题