Hi.

I have a limited area to display some text so I would like to count to the nth character, make sure it lands on an empty space, and insert 3 periods. However, I do not want to junk the rest of the text that comes after the nth character because my html tags will be stripped. I'm thinking the best way to do this is to set a CSS overflow to hidden at the specified height (which will need to coincide with the nth character).

I thought the best function to use is explode, but I cannot explode at a the nth character. If I use substr I lose the 2nd half of the text.

Any ideas?

Thanks

    Use substr twice?

    Er, I was going to write some code but you don't say: what if the nth character is not an empty space?

      I'm going to assume what you mean is that at the nth character, if it is not a space, continue up to a space, cut it off there and add the 3 dots?

      $str = 'This is a sample string that needs to be sampled!';
      preg_match('#^(.{13}[^ ]*)#', $str, $match); // assume nth spot is 13 characters long...
      echo $match[1] . '...';
      

      Output:

      This is a sample...
      

      In this case, the 13th character is the m in the word sample.. so the regex pattern will continue to capture anything that is not a space (zero or more times).. so 'ple' is also included with the capture to complete the word sample, then add the 3 dots... Is that what you are looking for?

      EDIT - Granted, if the nth character lands on a space, my preg solution will match all the way up to the next space.. perhaps this alternative is more accurate?

      $str = 'This is a sample string that needs to be sampled!';
      $cutOff = 13; // set this as the nth value
      if(substr($str, ($cutOff-1), 1) == ' '){ // nth position lands on a space
          $result = substr($str, 0, $cutOff);
      } else { // nth position does not land on a space
          $findSpace = strpos(substr($str, ($cutOff-1)), ' ');
          $result = substr($str, 0, ($cutOff + $findSpace));
      }
      echo $result .= '...';
      

        I realized in my second snippet sample that the space is included in the final variable $result.. so instead of using

        echo $result .= '...'; 
        

        you could use

        $result = rtrim($result) . '...';
        echo $result;
        

        This would in effect simply trim off the trailing space.

          Another way:

          $result = '';
          $result .= strtok($txt, ' ');
          while (strlen($result) < $n) {
              $result .= ' ' . strtok(' ');
          }
          $result .= '...';

            Thank you so much for the examples and explanations. Although the example provided by nrg_alpha meets the primary objective (to count to the nth character and if not an empty space, move to the next available empty space), it seems to remove all the text that comes after the nth character. So, how do we put it all back together?

            Basically, I would like to create this:

            This is a sample... string that needs to be sampled!

            and hide everything after ... (I plan to use the overflow:hidden property) Doing so will maintain all html tags, thus keeping the formatting and style intact. I am certainly open to other suggestions if this is not recommended.

            Thanks!

              $whole_thing = 'This is a sample string that needs to be sampled!';
              $n = 13;
              
              $first_part = strtok($whole_thing, ' '); 
              while (strlen($first_part) < $n) { 
                  $first_part .= ' ' . strtok(' '); 
              } 
              $first_part .= '...';
              $last_part = strtok('');
              
              echo 'First part: ' . $first_part . '<br />';
              echo 'Last part: ' . $last_part . '<br />';
              echo 'Whole thing: ' . $whole_thing . '<br />';

              Output:

              First part: This is a sample...
              Last part: string that needs to be sampled!
              Whole thing: This is a sample string that needs to be sampled!
              

                Awesome. I can't believe how short the code is to make this work!

                Before I mark this thread resolved, I just want to make sure that I understand all the code.

                $first_part = strtok($whole_thing, ' '); // THIS SPLITS THE STRING AT THE FIRST EMPTY SPACE
                while (strlen($first_part) < $n) { // WHILE THE FIRST PART IS LESS THAN THE LENGTH, HOP IN
                    $first_part .= ' ' . strtok(' '); // TAKE ALL THE CHARACTERS TO THE NEXT EMPTY SPACE AND ADD IT TO $first_part... 
                How does the the strtok know that we are working from $whole_thing? I will guess that it is using what is left from $whole_thing somehow...  
                } $first_part .= '...'; // ADD THE PERIODS TO THE END OF THE STRING $last_part = strtok(''); // ??

                Thanks,

                  strtok() is "initialized' the first time it's called in a script, with both the input string and the character to seek. Subsequently, unless you wish it to operate on a different string, only the seek character is given, which need not be the same every time. Or characters; you can give it a string for the seek argument and it will search until it finds one of the characters in that string. In the last line, passing an empty character cause strtok() to seek to the end of the input string (strtok('')) .

                  Good luck.

                    Write a Reply...