2

我正在尝试从数据库文本条目中检索第一句和最后一句。

我的代码在这个例子中运行良好:

$text = "He was doing ok so far, but this one had stumped him. He was a bit lost..."

功能:

function first_sentence($content) {  
 $pos = strpos($content, '.');  
 if($pos === false) {  
  return $content;  
  }  
 else {  
 return substr($content, 0, $pos+1);  
 }  
} // end function

// Get the last sentence

function last_sentence($content) {
 $content = array_pop(array_filter(explode('.', $content), 'trim'));
 return $content;
} // end function

最后一个句子功能考虑到句子末尾的任何尾随...,但两者都无法处理以下情况:

$text = "Dr. Know-all was a coding master, he knew everything and was reputed the world over. But the Dr. was in trouble..."

结果:第一句话:Dr. 最后一句话:有麻烦了

我需要修改函数以考虑“博士”之类的内容。和其他这样的缩写,如果可能的话,所以最后一个文本变量会出来:

第一句话:Dr. Know-all 是个编程高手,他什么都懂,名扬四海 最后一句话:但是 Dr. 有麻烦了

可以做到吗?非常感谢任何帮助!

4

4 回答 4

2

您可以通过替换它们来排除某些单词。

<?

function first_sentence($content) {  
 $pos = strpos($content, '.');  
 if($pos === false) {  
  return $content;  
  }  
 else {  
 return substr($content, 0, $pos+1);  
 }  
} // end function

// Get the last sentence

function last_sentence($content) {
 $content = array_pop(array_filter(explode('.', $content), 'trim'));
 return $content;
} // end function

$text = "Dr. Know-all was a coding master, he knew everything and was reputed the world over. But the Dr. was in trouble...";

$tmp = str_replace("Dr.","Dr____",$text);
echo $tmm ."\n"; 
echo str_replace("Dr____","Dr.",first_sentence($tmp ))."\n";
echo str_replace("Dr____","Dr.",last_sentence($tmp ));

?>

工作代码

于 2013-10-21T11:04:54.217 回答
1

或许你有想过。。

你能$content在搜索句子之前创建一个函数来编码/解码吗?

function encode_content($content){
    return $encoded_content = str_replace("Dr.", "Dr#;#", $content);
}

检索句子后,再次解码:

function decode_content($content){
    return $encoded_content = str_replace("Dr#;#", "Dr." , $content);
}
于 2013-10-21T10:57:17.050 回答
0

您可以检查您的substr长度,仅当长度超过 3 个字符(包括点)时才返回。如果它小于或等于,您可以使用白名单,以免偶然发现诸如“No”、“Me”、“Us”、“Oh”之类的词......拼字游戏词典应该可以帮助你:)

于 2013-10-21T10:57:02.793 回答
0

鉴于到目前为止的答案,只是在将一些新功能放在一起后回答我自己的问题

function encode_text($content){
  $search = array("Dr.", "i.e.", "Mr.", "Mrs.", "Ms."); // put our potential problems in an array
  $replace = array("Dr#;#", "i#e#", "Mr#;#", "Mrs#;#", "Ms#;#"); // make them good for first and last sentence functions
  $encoded_content = str_replace($search, $replace, $content);
  return $encoded_content;
} // end encode

然后我们只是交换搜索和替换变量来创建我们的解码函数。现在它们可以与上面的第一个和最后一个句子功能一起使用,它很有魅力。向数组添加东西很简单,考虑添加什么是合适的:)

干杯!

于 2013-10-25T14:29:27.683 回答