Jump to content

Recommended Posts

My code

--------

<?php

if ($_POST['Submit'])

{

$para = $_POST['para'];

}

$sentence = split('[.!?]',$para);

foreach ($sentence as $final)

if (empty($final))

    continue;

 

else {

$final = ltrim($final,'"');

$final = ltrim($final,',');

$final = ltrim($final,'\"');

$final = ltrim($final,'S');

$final = ltrim($final,'.');

 

 

if(strlen($final) <= 120)

echo $final.'.<br />';

else

echo substr($final,0,120). '...Read More.<br />';

}

}

?>

 

I am trying to input a paragraph and split it into sentences.

Some output sentences are proper and some are having errors

 

Few output error sentences are starting like this

--------------------------------------------

1).          //A sentence cannot start with a period

 

2)\"There are ....      //A sentence cannot start with \"

 

Link to comment
https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/
Share on other sites

ltrim is not working.

 

I also tried

{

....

$sentences = split('[.!?]',$para);

 

$sentences = ltrim($sentences,'\"');

 

i also tried

 

$bad = array("\xc2","\x80","\x8c","\x9d","\x98","\x99");

$find[] = '“';  // left side double smart quote

$find[] = 'â€';  // right side double smart quote

  $find[] = '‘';  // left side single smart quote

$find[] = '’';  // right side single smart quote

$find[] = '…';  // elipsis

$find[] = '—';  // em dash

$find[] = '–';  // en dash

 

  $replace[] = '"';

  $replace[] = '"';

$replace[] = "'";

  $replace[] = "'";

  $replace[] = "...";

  $replace[] = "-";

  $replace[] = "-";

 

foreach ($sentences as $final)

{

str_replace($find,$replace,$final);

str_replace($bad,"",$final);

 

I TRIED EVERYTHING, BUT THE QUOTES DOESN'T SEEM TO BE GOING FROM MY SENTENCES FORMED...

 

Works fine for me

$string = array(".A sentence cannot start with a period",'"A sentence cannot start with " (quote)');
foreach($string as $s){
$s = ltrim($s,".\"");
echo "$s<br>\n";
}

 

A sentence cannot start with a period<br>

A sentence cannot start with " (quote)<br>

 

 

 

WHALE

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.