0

私の最後の質問から導きます:

データベース php からの検索結果の整理

私の調査により、検索結果を切り捨て、キーワードを強調表示し、キーワードの左右に多数の文字を表示する次のコードにたどり着きました。見栄えが良くなりました。

私の質問は、以下のコードで、キーワードの左または右に文字を追加することで、単語を半分にカットすることです。

例:

Savings plans

..le="background: #E13300;">investing in international accounts is no longer the premise of the rich and famous, all expatriates living abroad can now enjoy flex...

切り捨て関数が単語とタグを半分にカットしないようにするにはどうすればよいですか????

コード:

 $word = 'invest'; 
$characters_before="80";
$characters_after="80";
function supertruncate($text, $word, $characters_before, $characters_after){
                  $pos = strpos($text, $word);
    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    $len = $pos + strlen($word) + $characters_after - $start;
                   $text = str_ireplace($word, '<span class="highlight" style="background: #E13300;">' . $word . '</span>', $text);
    return substr($text, $start, $len);
}
4

1 に答える 1

0

最初に切り捨てます。次にハイライトします。

$text = substr($text, $start, $len);
return str_ireplace($word, '<span class="highlight" style="background: #E13300;">' . $word . '</span>', $text);
于 2011-10-30T11:30:01.347 に答える