Limit the text to 400 characters?

Discussion in 'PHP' started by Lonny.P, Jul 26, 2006.

  1. #1
    Hello there

    I have a code here that gets the decription of a certain product out of a database.

    How can I limit the output to 400 characters?


    Here is the code:

    <?php
      if ($row_inventory["v_descr"])
      {
      echo $row_inventory["v_descr"];
      } 
      else
      {
      echo 'N/A';
      }
      ?> 
    PHP:
     
    Lonny.P, Jul 26, 2006 IP
  2. pierre

    pierre Active Member

    Messages:
    71
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    98
    #2
    pierre, Jul 26, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can also use mysql's substring function to just pick 400 characters. Saves on bandwidth from the database.
     
    T0PS3O, Jul 26, 2006 IP
  4. Lonny.P

    Lonny.P Peon

    Messages:
    298
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How do I do that? I have no knowledge in PHP...
     
    Lonny.P, Jul 26, 2006 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Paste the code that pulls the data from the database... Looks like "SELECT * FROM products WHERE products_id = 'N'.
     
    T0PS3O, Jul 26, 2006 IP
  6. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Get the first N characters is not always the best choice because some time the word is incomplete. You can think of first 20 words, for example.
    However, in this case, the mySQL query is like:

    $query="SELECT SUBSTRING( article_text, 1, 400 ) AS shorttext
    FROM articles
    WHERE article_id=101";
    PHP:
     
    goldensea80, Jul 27, 2006 IP