KBD

Keith Devens .com

Friday, August 29, 2008 Flag waving
"It's no trick for talented people to be interesting, but it's a gift to be interested. We want... – Randy S. Nelson (dean of Pixar University)
← Some more stuff on the "Bush lied" memeZFS →

Daily link icon Thursday, November 17, 2005

"Clip words" function in PHP

I've found this simple clipWords function I wrote to be really handy. I use it for my quotes thing to generate page titles and such. Check your browser bar on this page, for example.

<?php
function clipWords($str$word_count){
    
preg_match('/^\W*(\b\w+\b\W*){0,'.$word_count.'}/'$str$matches);
    return 
trim($matches[0]);
}
?>

You can use code like this to generate an elipsis:

<?php
$short 
clipWords($full7);
if(
strlen($short) < strlen($full))
    
$short .= '...';
?>

Update: Or, alternately:

<?php
function clipWords($str$word_count){
    return 
join(' 'array_slice(preg_split('/\s+/'trim($str)), 0$word_count));
}
#or
function clipWords($str$word_count){
    return 
join(' 'array_slice(preg_split('/\s+/'trim($str), $word_count+1), 0, -1));
?>
← Some more stuff on the "Bush lied" memeZFS →

Comments XML gif

DJ Hannibal wrote:

The Nietzche quote:

"Battle not with monsters lest ye become a monster and if you gaze into the abyss the abyss gazes into you."

Gets cut down to "Battle not with monsters..." while some of the really long quotes don't get cut at all. What's up with that?

∴ DJ Hannibal | 17-Nov-2005 5:16pm est | #8689

Keith (http://keithdevens.com/) wrote:

No it doesn't: http://keithdevens.com/quotes/Nietzsche.monsters

And I went through my quotes and clicked on pretty much every long one and didn't see one that didn't get cut correctly. Do you have a link to one that didn't work right?

Keith | 17-Nov-2005 5:23pm est | http://keithdevens.com/ | #8690

Keith (http://keithdevens.com/) wrote:

Ah, did you mean on my random quote rotation at the top of the page? Until just now I was actually using a different "clipChars" function that tried to use a similar (but more complicated) regular expression to cut at a number of chars, but which made sure not to cut in the middle of a word. I wanted to cut at chars because I figured it would be more likely to give a more regular length (which was appropriate for my header) than cutting at words, but I've just now replaced it with clipWords and it works just fine.

Keith | 18-Nov-2005 10:22am est | http://keithdevens.com/ | #8696

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 3 posts

Recent comments XML

new⇒WoW Forums -> Tanking in Wrath of the Lich King

 cheap wow gold in www.wow4s.com or​www.agamegold.com.and cheap​powerleveli...

74.53.254.179: Aug 29, 11:19pm

new⇒Johnny Walker Blue Label

Wow, thanks for the scotch review​:D

Lagavulin and Laphroaig are​some of...

Keith: Aug 29, 3:35pm

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.131s.

(Used 8 db queries)

mobile phone