[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


PHP Word count and sentence  break

Posted on 2006-03-30
Medium Priority
Last Modified: 2013-12-12
I have a form that I want to submit a Large set of text into.

I want to break it into paragraphs.

So I want to count in 3 -5 Sentences  add a line break and put in a word like BREAK or #BREAK# in between each set then echo it back to the screen.

How would this be done?
A sentence ends with a      .    or a ! or a ?
Also how can I show a total word count of the input text?

Here is the form that I have that submits the data.

<title>paragraph burst</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<form name="form1" method="post" action="<? $phpself; ?>">
  <p> Insert Text<br>
    <textarea name="original_text" cols="150"></textarea>
  <p>min Sentence
    <input name="min" type="text" value="3" size="4">
    Max sentence
    <input name="max" type="text" value="5" size="4">
    Break Token
    <input name="break_name" type="text" value="#BREAK#">
    <input type="submit" name="Submit" value="Submit">

I hope that this is easy..
Question by:jbrashear72

Accepted Solution

JB04 earned 2000 total points
ID: 16341632
I dont full understand your question, however to wrap a line of text use wordwrap

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;


to count the characters use the strlen function

$test = 'hello';

echo strlen($test); - outputs 5

Expert Comment

ID: 16347616
you can use <a href="http://us2.php.net/manual/en/function.preg-split.php">preg_split()</a>

and a perl regexp to split it into arrays of sentances something like preg_split("[.!?]",$mytext)

then output then from the array as use feel the need

foreach( $array as $key => $value){}

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

872 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