PHP - 获取文本的前两句话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4692047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - get first two sentences of a text?
提问by anonymous
My variable $content
contains my text. I want to create an excerpt from $content
and display the first sentence and if the sentence is shorter than 15 characters, I would like to display the second sentence.
我的变量$content
包含我的文本。我想从$content
第一个句子中创建一个摘录并显示,如果句子短于 15 个字符,我想显示第二个句子。
I've already tried stripping first 50 characters from the file, and it works:
我已经尝试从文件中删除前 50 个字符,并且它有效:
<?php echo substr($content, 0, 50); ?>
But I'm not happy with results (I don't want any words to be cut).
但我对结果不满意(我不想删减任何话)。
Is there a PHP function getting the whole words/sentences, not only substr?
是否有一个 PHP 函数可以获取整个单词/句子,而不仅仅是 substr?
Thanks a lot!
非常感谢!
回答by anonymous
I figured it out and it was pretty simple though:
我想出来了,虽然很简单:
<?php
$content = "My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. ";
$dot = ".";
$position = stripos ($content, $dot); //find first dot position
if($position) { //if there's a dot in our soruce text do
$offset = $position + 1; //prepare offset
$position2 = stripos ($content, $dot, $offset); //find second dot using offset
$first_two = substr($content, 0, $position2); //put two first sentences under $first_two
echo $first_two . '.'; //add a dot
}
else { //if there are no dots
//do nothing
}
?>
回答by broox
Here's a quick helper method that I wrote to get the first N
sentences of a given body of text. It takes periods, question marks, and exclamation points into account and defaults to 2 sentences.
这是我编写的一个快速帮助方法,用于获取N
给定文本正文的第一句话。它考虑了句号、问号和感叹号,默认为 2 个句子。
function tease($body, $sentencesToDisplay = 2) {
$nakedBody = preg_replace('/\s+/',' ',strip_tags($body));
$sentences = preg_split('/(\.|\?|\!)(\s)/',$nakedBody);
if (count($sentences) <= $sentencesToDisplay)
return $nakedBody;
$stopAt = 0;
foreach ($sentences as $i => $sentence) {
$stopAt += strlen($sentence);
if ($i >= $sentencesToDisplay - 1)
break;
}
$stopAt += ($sentencesToDisplay * 2);
return trim(substr($nakedBody, 0, $stopAt));
}
回答by mathius1
I know this is an old post but I was looking for the same thing.
我知道这是一个旧帖子,但我一直在寻找同样的东西。
preg_match('/^([^.!?]*[\.!?]+){0,2}/', strip_tags($text), $abstract);
echo $abstract[0];
回答by Paul
There is one for words - wordwrap
有一个词 - wordwrap
Example Code:
示例代码:
<?php
for ($i = 10; $i < 26; $i++) {
$wrappedtext = wordwrap("Lorem ipsum dolor sit amet", $i, "\n");
echo substr($wrappedtext, 0, strpos($wrappedtext, "\n")) . "\n";
}
Output:
输出:
Lorem
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor sit
Lorem ipsum dolor sit
Lorem ipsum dolor sit
Lorem ipsum dolor sit
Lorem ipsum dolor sit
回答by Michael Irigoyen
I wrote a function to do something similar to this on one of our websites. I'm sure it could be tweaked to get your exact result out of it.
我写了一个函数来在我们的一个网站上做类似的事情。我确信可以对其进行调整以获得您的确切结果。
Basically, you give it a string of text and the amount of words you want to have it trim to. It will then trim to that amount of words. If the last word it finds doesn't end the sentence, it will continue over the amount of words you specified until it reaches the end of the sentence. Hope it helps!
基本上,你给它一串文本和你想要修剪的单词数量。然后它会修剪到那个数量的单词。如果它找到的最后一个单词没有结束句子,它将继续你指定的单词数量,直到它到达句子的结尾。希望能帮助到你!
//This function intelligently trims a body of text to a certain
//number of words, but will not break a sentence.
function smart_trim($string, $truncation) {
$matches = preg_split("/\s+/", $string);
$count = count($matches);
if($count > $truncation) {
//Grab the last word; we need to determine if
//it is the end of the sentence or not
$last_word = strip_tags($matches[$truncation-1]);
$lw_count = strlen($last_word);
//The last word in our truncation has a sentence ender
if($last_word[$lw_count-1] == "." || $last_word[$lw_count-1] == "?" || $last_word[$lw_count-1] == "!") {
for($i=$truncation;$i<$count;$i++) {
unset($matches[$i]);
}
//The last word in our truncation doesn't have a sentence ender, find the next one
} else {
//Check each word following the last word until
//we determine a sentence's ending
for($i=($truncation);$i<$count;$i++) {
if($ending_found != TRUE) {
$len = strlen(strip_tags($matches[$i]));
if($matches[$i][$len-1] == "." || $matches[$i][$len-1] == "?" || $matches[$i][$len-1] == "!") {
//Test to see if the next word starts with a capital
if($matches[$i+1][0] == strtoupper($matches[$i+1][0])) {
$ending_found = TRUE;
}
}
} else {
unset($matches[$i]);
}
}
}
//Check to make sure we still have a closing <p> tag at the end
$body = implode(' ', $matches);
if(substr($body, -4) != "</p>") {
$body = $body."</p>";
}
return $body;
} else {
return $string;
}
}
回答by michalzuber
For me the following worked:
对我来说,以下工作有效:
$sentences = 2;
echo implode('. ', array_slice(explode('.', $string), 0, $sentences)) . '.';
回答by Matt Lowden
This would make sure it never returned a half-word;
这将确保它永远不会返回半字;
$short = substr($content, 0, 100);
$short = explode(' ', $short);
array_pop($short);
$short = implode(' ', $short);
print $short;
回答by Glen Solsberry
Here's a function modified from another I found online; it strips out any HTML, and cleans up some funky MS characters first; it then adds in an optional ellipsis character to the content to show that it's been shortened. It correctly splits at a word, so you won't have seemingly random characters;
这是我在网上找到的另一个功能修改后的功能;它删除任何 HTML,并首先清理一些时髦的 MS 字符;然后它会在内容中添加一个可选的省略号字符以表明它已被缩短。它正确地分割成一个词,所以你不会有看似随机的字符;
/**
* Function to ellipse-ify text to a specific length
*
* @param string $text The text to be ellipsified
* @param int $max The maximum number of characters (to the word) that should be allowed
* @param string $append The text to append to $text
* @return string The shortened text
* @author Brenley Dueck
* @link http://www.brenelz.com/blog/2008/12/14/creating-an-ellipsis-in-php/
*/
function ellipsis($text, $max=100, $append='…') {
if (strlen($text) <= $max) return $text;
$replacements = array(
'|<br /><br />|' => ' ',
'| |' => ' ',
'|’|' => '\'',
'|‘|' => '\'',
'|“|' => '"',
'|”|' => '"',
);
$patterns = array_keys($replacements);
$replacements = array_values($replacements);
$text = preg_replace($patterns, $replacements, $text); // convert double newlines to spaces
$text = strip_tags($text); // remove any html. we *only* want text
$out = substr($text, 0, $max);
if (strpos($text, ' ') === false) return $out.$append;
return preg_replace('/(\W)&(\W)/', '&', (preg_replace('/\W+$/', ' ', preg_replace('/\w+$/', '', $out)))) . $append;
}
Input:
输入:
<p class="body">The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What’s in it for me?</p>
<p>Kroger said the system, from Fujitsu,
<p class="body">The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What’s in it for me?</p>
<p>Kroger said the system, from Fujitsu,
Output:
输出:
The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What's in it for me? Kroger said the …
The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What's in it for me? Kroger said the …
回答by Roger
If I were you, I'd choose to pick only the first sentence.
如果我是你,我会选择只选第一句话。
$t='Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum justo eu leo.'; //input text
$fp=explode('. ',$t); //first phrase
echo $fp[0].'.'; //note I added the final ponctuation
This would simplyfy things a lot.
这会让事情变得简单很多。