Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 10 of 10

Thread: restricting number of letters

  1. #1

    restricting number of letters

    im writing a script to post the most recent posts on my forum and i want to keep the number of letters in the thread title to a certain point and if it goes over i want to put a ".." Can someone tell me how to do this? im using php

  2. #2
    simplist way that i can think of is using a string choppnig function like substr. ie:
    Code:
     
    echo substr($textToEcho, 0, 128);
    that will show the first 128 chars from the record

  3. #3
    http://www.kirupa.com/forum/showthread.php?t=86313


    PHP Code:
    <?php

    $sql 
    = ('SELECT SUBSTRING(title, 1, 16) AS title_short FROM blog_entry ORDER BY date DESC LIMIT 0, 8'); 

    ?>
    worked for me.

  4. #4
    this is checking string length and adding the "..."

    PHP Code:
    <?php
    // you want to truncate $myText
    $maxLength 150 // Max length of the string you want
    if (strlen($myText) > $maxLength) {
        
    $myText substr($myText,0,$maxLength);
        
    // we add the "..."
       
    $myText .= "..."
    }
    ?>

  5. #5
    learn something new every day. lol

    PHP Code:
    <?php
     
    $sql 
    = ('SELECT SUBSTRING(title, 1, 16) AS title_short FROM blog_entry ORDER BY date DESC LIMIT 0, 8'); 
     
    ?>

  6. #6
    Quote Originally Posted by four1seven
    http://www.kirupa.com/forum/showthread.php?t=86313


    PHP Code:
    <?php
     
    $sql 
    = ('SELECT SUBSTRING(title, 1, 16) AS title_short FROM blog_entry ORDER BY date DESC LIMIT 0, 8'); 
     
    ?>
    worked for me.

    BTW this will select the first 16 characters...


    and this is my echo -

    PHP Code:
     
    $sql 
    'SELECT SUBSTRING(title, 1, 16) AS title_short, entryNo FROM blog_entry ORDER BY entryNo DESC LIMIT 0, 8';
    $result mysql_query($sql) or die(mysql_error()); 
    if(
    mysql_num_rows($result)!= 0)

    while (
    $rows=mysql_fetch_array($result)){
    $id=$rows[entryNo];
    {
    echo 
    "<b>+</b>&nbsp;<a href=\"entry.php?recent=$id\">$rows[title_short]...</a><br />"

    As you can see i just added the "..." in the echo.

  7. #7
    thanks a lot guys, ill definitely be using one of those lol.

  8. #8
    ok im using this and its not adding the "..." at the end:

    PHP Code:
    $maxLength 15;// Max length of the string you want
    if (strlen($row[1]) > $maxLength) {
        
    $row[1] = substr($row[1],0,$maxLength);
        
    // we add the "..."
       
    $myText .= "...";
       } 

  9. #9
    PHP Code:
    $maxLength 15;// Max length of the string you want
    if (strlen($row[1]) > $maxLength) {
    $myText substr($row[1],0,$maxLength);
    // we add the "..."
    $myText .= "...";
    } else {
    $myText $row[1];

    Haven't checked it, but it looked like the reason it wasn't working was because you weren't defining $myText in the if

  10. #10
    aha, another dumb mistake by SR. thanks a lot man

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012