Jump to content

Shorten text... click and expand


wispas

Recommended Posts

Like the title says, i want to have a paragraph of text and shorten it to 25 characters... it will have a read more button and when clicked it will expand all of the text.

 

not sure if this is php... if so does anyone know what this is called and how this is done.

 

thanks!

Link to comment
Share on other sites

Like the title says, i want to have a paragraph of text and shorten it to 25 characters... it will have a read more button and when clicked it will expand all of the text.

 

not sure if this is php... if so does anyone know what this is called and how this is done.

 

thanks!

 

You can shorten it, and even add a link making it truly dynamic with PHP. But yeah, JS will be needed to expand the actual area.

$string = 'I am a long title, Shorten me to 30 characters and it will be interesting to see the result';

function shorten($string, $length, $id) {
if (strlen($string) > $length) {
$string = substr($string, 0, $length)."<a onClick='expandText(".$id.")'...</a>";
}

return $string;
}

$shorttitle = shorten($string, 30, 'div1');
echo $shortitle; 

 

The onClick code won't work, but I showed you an example of how you can pass the id of the string you want to shorten, or you can just define it yourself (simpler if you need be)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.