Jump to content

[SOLVED] Sentence Structures


JonnoTheDev

Recommended Posts

Anyone know of a library that can take in a string and auto-format the sentence structure. i.e.

 

Original

=========

this is a sample sentence. this is another sample sentence.

 

this is the start of a new paragraph.

 

New

=========

This is a sample sentence. This is another sample sentence.

 

This is the start of a new paragraph.

 

Link to comment
https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/
Share on other sites

That will only format the first charater of the text. Needs to be all sentences/paragraphs in the entire string (could be a whole article).

Also do not want to convert to lowercase, lets say the text had NASA in it. Dont want it coming out as nasa.

Ok so I had a concept in my mind of which is in this post...

 

http://objectmix.com/php/490027-re-php-sentence-case.html#post1757216

 

...It is the long way round, and some may criticise it for being inefficient with high volumes, but it seems t be your only answer.

 

ILMV

Sorted. Heres how I did it if you are interested:

 

$string = "chester is a county city in Cheshire, England, located in what is named the English Northwest. chester lies on the River Dee, a water body that borders Wales.";

// explode each paragraph
// each empty array value is a line break (carraige return)
$paragraphs = preg_split('/\\n{1,}/', $string, -1, PREG_SPLIT_NO_EMPTY);

// loop through each paragraph and explode the sentences
for($x = 0; $x < count($paragraphs); $x++) {
$stringParts = explode(". ", $paragraphs[$x]);
// loop through each sentence and capitalize the first letter
for($i = 0; $i < count($stringParts); $i++) {
	$stringParts[$i] = ucfirst($stringParts[$i]);
}
// implode the string parts back into sentences
$paragraphs[$x] = implode(". ", $stringParts);
}

// build back up the new string
foreach($paragraphs as $paragraph) {
$newString .= strlen($paragraph) ? $paragraph : "\n";
}

print nl2br($newString);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.