Jump to content
xisto Community
Sign in to follow this  
Amezis

Max Characters Is X?

Recommended Posts

I am currently making a code for my phpBB forum, where it will import the latest topics from a specific forum, and display it on the news section of my main page.But the width of that news section is very tiny, so I want the code to limit the numbers of characters to 25.So, let's say this is the full title:"This is the title which is a little too long so it needs to be shortened."This is what I want to show:"This is the title which ..."How can I do that?

Share this post


Link to post
Share on other sites

I haven't tested this, but it should work ;)

$max_length = 25;$title = "This is the title which is a little too long so it needs to be shortened.";$title_length = strlen($title);if ($title_length > $max_length){    $title = substr_replace($title, '...', $max_length);}echo $title;
It's pretty self explanitory. If you have any problems or it totally fails to work then feel free to ask!

Share this post


Link to post
Share on other sites

Thanks alot, it worked perfectly! ;)

But I have another question. If the title is too long, I want to add the title="fulltitlehere" attribute.

So, basically, my topic title is "This is the title which is a little too long so it needs to be shortened". It is then shortened to "This is the title which ...". But if it is shortened, I want to have the title="" attribute in the link too:

<a href="linktotopic" title="This is the title which is a little too long so it needs to be shortened">This is the title which ...</a>

I tried with this code (using your code to define $max_length):

if (($echo_text['post_subject']) > $max_length)    {    $titleattribute = ' title=\"' . ( $echo_text['post_subject'] ) . '\"'    }
$echo_text['post_subject'] is the complete topic title.

Hope you understand what I need ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.