Jump to content

Can we make it better code ??


salman_ahad@yahoo.com

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

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.