1

我有一个保存到 dbase 的文本字段,其中包含很长的电子邮件。如果预览部分说像只查看 250 个单词并放...或链接以查看其余部分,我想回应部分。

请帮我写代码

我用普通的

echo $row['email'];
4

3 回答 3

0
$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;

我想这就是你的意思?

于 2013-10-14T10:07:42.980 回答
0

您可以使用以下函数来自动换行(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;
}
于 2013-10-14T10:07:58.090 回答
0

我不知道你的完整代码是什么。但是,这个问题可以有多种解决方案。一种可能的解决方案是:

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

我测试了它,这对你来说很好。

于 2013-10-14T10:11:17.437 回答