Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts

    cut down a variable?

    Is there a way to take a 482 charecter variable and cut it down to like 100 charecters and add a ... on the end?


    Thanks,
    ILLINI
    Why not thank me?

    http://adamsworld.name

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could use substr, but if you have htmlentities you will need something like this(someone had passed this along to me):
    Code:
    substr($topic, 0, strrpos(substr($topic, 0, $chars_len), ' ')).'...';
    but you only need to do that if the string is greater then your max value.

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I find it much easier to have complete words, so you might cut it like:

    PHP Code:

    $str 
    'Blue boy, blue boy. What do you say? Blue boy, blue boy.';

    for(
    $i 0 $words explode($str), $word_max 6$i <= $word_max$i++) {
      echo 
    $words[$i];
      if(
    $i == $word_max) { echo '...'; }


  • #4
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts
    PHP Code:
    $str $description;
    for(
    $p 0 $words explode($str), $word_max 10$p <= $word_max$p++) {
      echo 
    $words[$p];
      if(
    $p == $word_max) { echo '...'; }

    Code:
    Parse error: parse error, unexpected T_VARIABLE, expecting ';' in C:\server\xampp\htdocs\css\maps.php on line 116
    116 is the line with the for() on it. Also This is already in a for loop... would that create problems? I changed the i's to p's so that it wont get the two mixed together.
    Why not thank me?

    http://adamsworld.name

  • #5
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is another method

    PHP Code:
    <?php
    function reverse_strrchr($haystack$needle)
    {
       
    $pos strrpos($haystack$needle);
       if(
    $pos === false) {
           return 
    $haystack;
       }
       return 
    substr($haystack0$pos 1);
    }

    function 
    trimContent($content){
        if(
    strlen($content) >150){
            
    $content substr($content,0,150);
            
    $content reverse_strrchr($content' ') . "..."
        }
        return 
    $content;
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $str 
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    echo 
    trimContent($str);
    ?>
    </body>
    </html>


  •  

    Posting Permissions

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