Jul 20, 2008

Max Characters Is X?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Max Characters Is X?

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

Reply

rvalkass
I haven't tested this, but it should work tongue.gif

CODE

$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!

Reply

Amezis
Thanks alot, it worked perfectly! biggrin.gif

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):

CODE
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 tongue.gif

 

 

 


Reply

electriic ink
Try:

CODE
if ($echo_text['post_subject'] > $max_length)
  {

  $titleattribute = ' title=\"' . $echo_text['post_subject']  . '\"';

  }

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : max characters

  1. Converting Textarea Return Characters To <br /> - (7)
  2. Validation Script - Detecting Illegal Characters - preg_match to find illegal characters (0)
    I'm trying to validate a user's desired username for a registration page. I want to detect
    any illegal characters being used for a username. So far i have managed to include most execpt the
    '\', the '\' is used for escape and treating the preceding characters
    literally. I try to do '\\' but i get an error because it execects another
    character. CODE $ck_result = "Default"; $pattern =
    "/[!|@|#|$|%|^|&|*|(|)|_|\-|=|+|\||,|.|\/|;|:|\'
    ;|\"|&#...
  3. Php Escaped Control Characters And Windows - they aren't working for me. (3)
    In php coding, when you escape certain characters, they have an impact on the output of an
    echo'd or printed string. ie "\r\n\t" should give you a new line and a tab before
    outputting the content for a line of printing. They vary by OS, I know, but I have tried all the
    variations I can think of and can't seem to control output using them. Believe me, I have tried
    multiple variations and nothing works for me. I have checked the php.ini file for settings but I
    can't find where you can modify them. So the question is : Where do i look to conf...
  4. Converting Characters In A Variable To Individual Values In An Array - turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ...
  5. Counting Characters Of Textarea... - (3)
    good day webmasters... i am in need of a script that will count the inputted characters one will
    enter... i mean the script must count the number of characters in a textarea as i am entering
    characters to the textarea.. i know the logic but i cant translate it yet in PHP... so i hope you
    can help me... thanks a lot in advance... /blink.gif' border='0' style='vertical-align:middle'
    alt='blink.gif' /> ...



Looking for max, characters, x

Searching Video's for max, characters, x
advertisement



Max Characters Is X?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE