Extracting n Number Of Words From String in PHP

This function will extract a number of words from a string. Uses a regular expression search to get the words.

function get_words($str,$count)
	return implode(" ", array_slice(preg_split("/\s+/", $str), 0, $count));

$string = "In Scott Pilgrim vs. the World, the titular character is definitely experiencing girl problems.";
echo get_words($string,10).'...';

This example will print out the first 10 words: ‘In Scott Pilgrim vs. the World, the titular character is…’

Leave a comment


  1. Its like you learn my mind! You seem to know a lot approximately this, like you wrote the book in it or something.
    I feel that you simply can do with a few % to pressure the message home a little bit, however other than that,
    this is magnificent blog. A great read. I’ll certainly be back.

  2. I think the admin of this website is actually working hard
    in favor of his website, since here every information is quality based information.

  3. Its not my first time to visit this site, i am browsing this site dailly and get pleasant information from here daily.

  4. If you’re in a dire situation, cut off the monthly
    subscriptions to your game. This list is your MONEY BLUEPRINT in
    written format. Whether such people get rich or work very hard
    trying to become successful, they usually aren’t happy.
    Each time, the live studio audience seems equally
    emotionally invested in the family’s dilemma.

    Seriously, look at the ground the entire time you aren’t fighting.

  5. Excellent way of explaining, and pleasant post to obtain facts
    concerning my presentation focus, which i am going to deliver in academy.

  6. Following a somewhat longer lean time period, we had many tablets under overview again. Fujitsu, HP, and Lenovo supplied us
    with House windows tablets – all of them with House windows 8.1 and
    a 10.1 inch display.

  7. set-in a brushed steel case and a steel bezel surrounding a white switch
    and silver coloured arms, its fitted with a black crocodile band.
    This watch additionally boasts an 18k white gold instance with mother-of-pearl face.

  8. Howdy! This blog pist couldn’t bee written much better!
    Reading through this article reminds me oof my previokus roommate!
    He continually kept preaching about this. I am going to
    forrward this article to him. Pretty sure he’s going
    to have a very good read. Thanks for sharing!

  9. Car Wash: It is indispensable to keep the car
    clean. Take a look at all of your local car washes, will they
    be choosing terry cloths to wash or dry your vehicle, are their particular brushes
    as soft as horsehair. The whole parking lot is going to be
    well lit and be fenced in so that no one can get anywhere near your car.

  10.  ( 2012.02.22 13:52 ) : I didn’t had a good read by a long time. Definitely happy I found this on digg. I was speaking toehtger with my buddies two nights ago all-around this particular issue and also I have to express you make issues really crystal clear for me. Thank you and good luck,

  11.  ( 2012.02.22 01:42 ) : Thank you for being my own instructor on this topic. My supose and i enjoyed your article quite definitely and most of all favored how you handled the issues I considered to be controversial. You’re always incredibly kind towards readers much like me and assist me in my life. Thank you.

  12.  ( 2012.02.18 01:19 ) : Great post. I used to be checking cnltiouousny this site and I’m inspired! Very helpful info specially the remainder part I handle these kinds of information much. I used to be seeking this type of information for some time. Thanks and best regarding luck. odloty

  13.  ( 2012.02.15 18:21 ) : Excellent I really suhold surely say I’m impressed along with your website. I had no trouble navigating through all the tabs and related information. The web site ended up being truly straightforward to access. Excellent job… obtaining insurance

  14. One thing I’d really like to say is that car insurance canceling is a feared experience and if you’re doing the suitable things being a driver you’ll not get one. A number of people do receive the notice that they have been officially dropped by their insurance company they have to scramble to get additional insurance after a cancellation. Affordable auto insurance rates usually are hard to get after having a cancellation. Knowing the main reasons pertaining to auto insurance cancelling can help motorists prevent losing one of the most essential privileges accessible. Thanks for the ideas shared via your blog.

  15. I simply wanted to thank you so much yet again. I do not know the things I would have implemented without these points shared by you over such a topic. It absolutely was a very difficult condition for me personally, but encountering the professional avenue you handled that forced me to weep for contentment. Now i’m happier for the help and in addition pray you really know what a great job you were providing educating other individuals via your webblog. Most likely you have never got to know all of us.

  16. Together along with everything which seems that they are biduling within this exacting affair substance, a sizeable percentage regarding details of view are generally preferably radical. But, I appologize, except My partner and i can indeed not subscribe used for your whole suggestion, every one of whether it is refreshing probably no one the less. It looks to ensue bright to everybody that your exacting opinions are commonly not necessarily finally streamlined down with in statement an distinctive are typically your do-it-yourself not absolutely undeniable involving your point. In more or less some incident Worn to achieve be pleased about evaluation the item.

  17. A slice of the things you asrest is amazingly legitimate and that makes me wonder the reason why I hadn’t looked by this in this light facing. This piece truly did alter the effortless resting on used for me as far as this distinct issue goes. Nevertheless by the side of this time here is really 1 arrange I am not too comfortable with and while I stab to reconcile that with the focal sketch of your arrange, authorize me observe immediately what all the rest of your visitors have to say.Well done.

  18. But this creamy tint really provides me cmfortable feeling on my lips.

  19. This article will probably turn out to be suggested with regards to Twitting as a result of a professional. truly?

  20. With work like this, ball tracking in video games like soccer, basketball, and volleyball is coming nearer.

  21. Excellent post. Keep posting such kind of information on your
    blog. Im really impressed by your site.
    Hi there, You’ve done a great job. I will definitely digg it and
    individually suggest to my friends. I am sure they will be benefited from this web site.