Hi,
I have a blog page and on this page I only want to show the first 250 characters of the main blog post. So let's say I have a variable called $text and in this contains "The quick brown fox jumps over the lazy dog". But what I want to show is "The quick brown fox..."
How would I go about doing that?
Thanks.
Shortening a string
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Shortening a string
I've been meaning to work through the logic on this sort of thing for a while. Here's an opportunity 
As you can see, you set the max characters. The limit comes down until the last character isn't a letter, and then cuts out the excerpt. That way, it will never break a word. The second condition makes sure it doesn't cut the whole thing out in odd situations.
I'm not quite sure if I got the indexing right: you might need to change it to substr($text, 0, $pos-1).
Edit: fixed missing semicolon
Code: Select all
$text = 'The quick brown fox jumps over the lazy dog';
$limit = 24;
$threshold = 10;
$pos = $limit;
while (!preg_match('/[^a-z]/i', $text[$pos]) && ($limit - $pos) < $threshold) {
$pos--;
}
$excerpt = substr($text, 0, $pos) . '...';I'm not quite sure if I got the indexing right: you might need to change it to substr($text, 0, $pos-1).
Edit: fixed missing semicolon
Last edited by Jonah Bron on Tue Sep 28, 2010 11:14 am, edited 1 time in total.
Re: Shortening a string
Perfect thanks!
Re: Shortening a string
I don't mean to diminish your work, Johan. I solved it differently in this excerpt function.
Re: Shortening a string
One thing to keep in mind is what type of editor is used for writing the blog. Does it take just plain text, or is it like TinyMCE, where it will give you actual formatted HTML.
If it is the later, do not forget to handle the fact that there are tags in there, and they will be counted as characters. While I don't have the exact code right now I use here is the gist of some of the extra steps it takes:
Ok, I have no idea why the list isn't working below, if an admin can edit it and let me know, it would be appreciated...)
(Oh, I also have code that will take into consideration things like & as 1 character, I have since found better methods, which is why I didn't put what I have here)
-Greg
If it is the later, do not forget to handle the fact that there are tags in there, and they will be counted as characters. While I don't have the exact code right now I use here is the gist of some of the extra steps it takes:
Ok, I have no idea why the list isn't working below, if an admin can edit it and let me know, it would be appreciated...)
- [First, replace any <li> tags with ~*~ (This is not the final replacement, just something most likely NOT to be in the content already)
- Now replace all >< (close of one tag up against the next opening one) with > < (add a space, <p>Sentence one.</p><p>Sentence two.</p> would come out as Sentence one.Sentence two.
- Strip out all HTML tags at this point
- Replace all whitespace characters (and multiples) with a single actual space
- Replace all ~*~ and optional space with the final output you want to display (I use (*))
(Oh, I also have code that will take into consideration things like & as 1 character, I have since found better methods, which is why I didn't put what I have here)
-Greg
Re: Shortening a string
You could also use strtok()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.