1

I have a text field saved to dbase with very long email. I want to echo part if it for preview say like view just 250 words and put... or link to view the remaining.

Please help me with the code

I use the normal

echo $row['email'];
  • What have you done so far? Post some code and you will get some help with it. – StBlade Oct 14 '13 at 10:01
  • Why do You want to allow so long email? The maximum length of an email address is 254 characters! Look at this: http://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address – Adam Oct 14 '13 at 10:09
  • I wasn't referring to email address but email message. As long as the history of America. – Sunej Global Oct 14 '13 at 10:44

3 Answers3

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;

I think this is what you mean?

Albzi
  • 15,431
  • 6
  • 46
  • 63
0

You can use the following functions to wordwrap(utf8-safe!) your text and create the link. wrap() splits your text into an array strings. That makes more sense than specifying the number of words, since words can be very short "hi" or very long "hippopotomonstrosesquipedaliophobia". dont forget to escape the output.

example: 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;
}
aton
  • 164
  • 1
  • 8
0

I don't know what your complete code is. But, there can be multiple solutions to this question. One probable solution can be:

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

I tested it, this will work fine for you.

nurakantech
  • 502
  • 4
  • 14