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!

 

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.