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'];
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'];
$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?
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;
}
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.