完全な文字列を切り捨てるときにSEARCH TERMの前後の文字数を厳密にカウントする検索結果があります。残念なことに、これにより出力は途中の単語を切り捨てます。 (...前後の楕円でカウントする)PHP - 単語を切り捨てずに出力を表示
私は、検索結果を空白でのみ、単語の真ん中で完全な文字列を切り詰めようとしています。ここで
機能です:ちょうど文字列出力、楕円「を探す」という機能を使って、文字列によるスクリプトべきループを表示する前に...
private function _highlight_results(){
$GLOBALS['_SEARCH_SUMMARY_LENGTH'] = 24;
foreach($this->results as $url => &$this_result){
if(!$this_result['url_display'] && $this_result['url']){
$this_result['url_display'] = $this_result['url'];
}
foreach($this_result['search_term'] as $search_term){
$search_term = preg_quote($search_term,'/');
foreach(array('title','summary','url_display') as $highlight_item){
if($this_result[$highlight_item] && preg_match('/'.$search_term.'/i',$this_result[$highlight_item])){
if($highlight_item != 'url_display' && strlen($this_result[$highlight_item]) > $GLOBALS['_SEARCH_SUMMARY_LENGTH']){
$boobs = ceil(($GLOBALS['_SEARCH_SUMMARY_LENGTH']-strlen($this->_search_term))/2);
preg_match('/(.{0,'.$boobs.'})('.$search_term.')(.{0,'.$boobs.'})/i',$this_result[$highlight_item],$matches);
// want to even out the strings a bit so if highlighted term is at end of string, put more characters infront.
$before_limit = $after_limit = ($boobs - 2);
if(strlen($matches[1])>=$before_limit && strlen($matches[3])>=$after_limit){
// leave limit alone.
}else if(strlen($matches[1])<$before_limit){
$after_limit += $before_limit - strlen($matches[1]);
$before_limit = strlen($matches[1]);
preg_match('/(.{0,'.($before_limit+2).'})('.$search_term.')(.{0,'.($after_limit+2).'})/i',$this_result[$highlight_item],$matches);
}else if(strlen($matches[3])<$after_limit){
$before_limit += $after_limit - strlen($matches[3]);
$after_limit = strlen($matches[3]);
preg_match('/(.{0,'.($before_limit+2).'})('.$search_term.')(.{0,'.($after_limit+2).'})/i',$this_result[$highlight_item],$matches);
}
$this_result[$highlight_item] = (strlen($matches[1])>$before_limit) ? '...'.substr($matches[1],-$before_limit) : $matches[1];
$this_result[$highlight_item] .= $matches[2];
$this_result[$highlight_item] .= (strlen($matches[3])>$after_limit) ? substr($matches[3],0,$after_limit).'...' : $matches[3];
}
}else if(strlen($this_result[$highlight_item]) > $GLOBALS['_SEARCH_SUMMARY_LENGTH']){
$this_result[$highlight_item] = substr($this_result[$highlight_item],0,$GLOBALS['_SEARCH_SUMMARY_LENGTH']).'...';
}
}
}
foreach($this_result['search_term'] as $search_term){
$search_term = preg_quote($search_term,'/');
foreach(array('title','summary','url_display') as $highlight_item){
$this_result[$highlight_item] = preg_replace('/'.$search_term.'/i','<span id="phpsearch_resultHighlight">$0</span>',$this_result[$highlight_item]);
}
}
}
}
は、ここで私が考えていたものだ と直後の文字を返し、その後にAFTERという文字を削除し、空白が見つかるまでループを続けます。次に、次のループは文字を探し、次に楕円を探して文字を削除し、楕円の前に空白が見つかるまでループを続けます。私はあなたが私は上記のものから探しているもののアイデアを得ることができると思い
WHILE (not the end of the string) {
// NOT SURE IF I NEED A FOREACH LOOP HERE TO CHECK EACH CHAR
IF (^ ('...' and an immediate char are found)) {
delete chars until a white space is found;
// if '...' is deleted along with the chars, then put the '...' back in:
//string .= '...' . string;
}
IF ($ (a char and an immediate '...' are found)) {
delete chars until a white space is found;
// if '...' is deleted along with the chars, then put the '...' back in:
//string .= string . '...';
}
}
PRINT string;
:ここ
は、上記の私の記述のいくつかの非常に悲しい擬似コードです。私はwordwrap()を研究し、テストしましたが、答えはまだ見つかりませんでした。
このサイトの正規表現でwordwrapを検索することをお勧めします。類似しているので、いくつかのポインタが必要です。 – hakre
このリンクを試してみると... http://stackoverflow.com/a/26098951/3944217 – edCoder