salman_ahad@yahoo.com Posted October 26, 2009 Share Posted October 26, 2009 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 \" Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/ Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 26, 2009 Author Share Posted October 26, 2009 Any help ???? Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/#findComment-944838 Share on other sites More sharing options...
lemmin Posted October 26, 2009 Share Posted October 26, 2009 Try using explode() $sentences = explode(". ", $paragraph); Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/#findComment-944841 Share on other sites More sharing options...
MadTechie Posted October 26, 2009 Share Posted October 26, 2009 Read ltrim in the manual charlist You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters. Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/#findComment-944852 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 26, 2009 Author Share Posted October 26, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/#findComment-945055 Share on other sites More sharing options...
MadTechie Posted October 27, 2009 Share Posted October 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179005-can-we-make-it-better-code/#findComment-945084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.