473,408 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

character_limiter

vikas1111
122 100+
Hi all,
I want to print content of post but limited portion and after that i show a link “Read More” in end so that it leads to full post detail. But when i use substr() func and use 200 character it prints well but i need to print or need to end substring with full word

example:

Default:
This is my post and when it mak…

I need:
This is my post and when it makes…

I know there is some function in php but i really dont remember.

thanks in advance:
Jul 22 '09 #1
6 2795
vikas1111
122 100+
This is one solution i got after little bit of Googling. I am looking for the function which can replace bellow lines of code.

Expand|Select|Wrap|Line Numbers
  1. $position=14; // Define how many characters you want to display.
  2.  
  3. $message="gg rier j ajajjawer wjrwj rejwjre wjrwjre jwejr"; 
  4. $post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message. 
  5.  
  6. if($post !=" "){ // In this step, if last character is not " "(space) do this step . 
  7.  
  8. // Find until we found that last character is " "(space) 
  9. // by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20) 
  10. while($post !=" "){
  11. $i=1;
  12. $position=$position+$i; 
  13.  
  14. $message="gg rier j ajajjawer wjrwj rejwjre wjrwjre jwejr"; 
  15. $post = substr($message,$position,1); 
  16. }
  17.  
  18. }
  19.  
  20. $post = substr($message,0,$position); // Display your message
  21. echo $post;
  22. echo "...";
Jul 22 '09 #2
Dormilich
8,658 Expert Mod 8TB
wouldn't it be easier to look for the first space after a specified length/offset?
Expand|Select|Wrap|Line Numbers
  1. // using a 50 chars minimum length
  2. $cut = strpos($text, ' ', 50);
  3. // now using $cut in substr()
Jul 22 '09 #3
vikas1111
122 100+
@Dormilich
Hi Dormilich,

Thanks for reply. Yes its a good idea it works.

Kind Regards
Jul 22 '09 #4
Atli
5,058 Expert 4TB
Wouldn't it be better to cut the text of before reaching the limit?

Expand|Select|Wrap|Line Numbers
  1. $words = explode(" ", substr($post, 50));
  2. array_pop($words);
  3. echo implode(" ", $words);
  4.  
I use this in a page where the text must not exceed the limit.
Jul 22 '09 #5
Dormilich
8,658 Expert Mod 8TB
it would be also possible to cut at 50 (or whatever value) chars and use strrpos() to find the last space.
Jul 22 '09 #6
Atli
5,058 Expert 4TB
@Dormilich
Good point, and that might even be more efficient then my array method.
I'll have to look into that. Thanks! :-)
Jul 22 '09 #7

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.