?
Solved

php script to get first x words of string

Posted on 2013-11-26
2
Medium Priority
?
271 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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 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