高级的第一句和最后一句功能

时间:2013-10-21 10:47:40

标签: php function

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

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

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

结果: 第一句话:博士 最后一句话:遇到了麻烦

我需要修改功能以考虑'博士'这样的事情如果可能的话,还有其他这样的缩写,所以最后一个文本变量将表示为:

第一句话:Know-all博士是一位编码大师,他知道一切,并在全世界享有盛名 最后一句:但是博士遇到了麻烦

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

4 个答案:

答案 0 :(得分:2)

您可以在replacing之前排除某些字词。

<?

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 ));

?>

WORKING CODE

答案 1 :(得分: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);
}

答案 2 :(得分:0)

您可以检查substr长度,只有长度超过3个字符(包括点)才能返回。如果它小于或等于,你可以使用白名单,以免偶然发现诸如“不”,“我”,“我们”,“哦”等字样......拼字游戏词典应该能够帮助你:)

答案 3 :(得分: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

然后我们只是交换搜索并替换变量来制作我们的解码功能。现在它们可以与上面的第一个和最后一个句子函数一起使用,并且它具有魅力。将数据添加到数组很简单,考虑添加什么是合适的,不如此:)

干杯!