非常に長い電子メールを含むテキスト フィールドを dbase に保存しています。プレビュー用に 250 語だけを表示して置きます... またはリンクして残りを表示する場合は、一部をエコーしたいです。
コードを教えてください
普通に使ってる
echo $row['email'];
あなたの完全なコードが何であるかわかりません。ただし、この質問には複数の解決策が考えられます。考えられる解決策の 1 つは次のとおりです。
<?php
//test.php
//just this part only,
$step = isset($_REQUEST['step'])?(int)$_REQUEST['step']:1;
if($step==1):
echo substr($row['email'],0,250);
echo '<a href="test.php?step=2" target="_self">view more</a>';
elseif($step==2):
echo substr($row['email'],250,strlen($row['email']));//if you want to display the rest
//if you want to display the whole text simply echo $row['email']
endif;
?>
私はそれをテストしました、これはあなたにとってうまくいくでしょう。
$email= $row['email'];
if (strlen($email) > 250) {
// cut the email string
$emailCut= substr($email, 0, 250);
//ensure that it ends with a whole word
$email= substr($emailCut, 0, strrpos($emailCut, ' ')).'... <a href="#">Read More</a>';
}
echo $email;
これがあなたの言いたいことだと思いますか?
次の関数を使用して、テキストをワードラップ (utf8-safe!) し、リンクを作成できます。wrap() は、テキストを配列文字列に分割します。単語は非常に短い "hi" または非常に長い "hippopotomonstrosesquipedaliophobia" になる可能性があるため、単語の数を指定するよりも意味があります。出力をエスケープすることを忘れないでください。
例:echo wrappedlink(htmlspecialchars($row['email']), 20);
function wrappedlink($str, $len) {
return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n".
'<a href="javascript:toggleDisplay(\''.($id=substr(md5(rand().$str),0,8)).'\');">'.wrap($str, $len)[0].'</a> <div id="'.$id.'" style="display:none;">'.$str.'</div>';
}
function wrap($string, $width) {
if (($len=mb_strlen($string, 'UTF-8')) <= $width) return array(
$string
);
$return=array();
$last_space=FALSE;
$i=0;
do {
if (mb_substr($string, $i, 1, 'UTF-8') == ' ') $last_space=$i;
if ($i > $width) {
$last_space=($last_space == 0)?$width:$last_space;
$return[]=trim(mb_substr($string, 0, $last_space, 'UTF-8'));
$string=mb_substr($string, $last_space, $len, 'UTF-8');
$len=mb_strlen($string, 'UTF-8');
$i=0;
}
$i++;
} while ($i < $len);
$return[]=trim($string);
return $return;
}