?
Solved

php script to get first x words of string

Posted on 2013-11-26
2
Medium Priority
?
272 Views
Last Modified: 2013-11-26
$string='This, is a big test. This is not a big test';

$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
       '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
       '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
       '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
                        );
$text = preg_replace($search, '', $string);
$words = str_word_count($text, 2);
return array_slice( $words, 0, 7);


result='This is a big test This is';

Issue
this script removes the fullstop and coma

What I want to return is 'This, is a big test. This is'
.... maintain the coma and fullstop

Can you help?
0
Comment
Question by:joomla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1000 total points
ID: 39679992
$words = str_word_count($text, 2, '.,');
0
 

Author Closing Comment

by:joomla
ID: 39679997
thanks ozo,
i could have saved myself a lot of time by actually reading the manual.
thanks for your help
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question