Jump to content

PHP stop sentence to . ? !


Mudsaf

Recommended Posts

Hello, i'm stuck on my code and can't seem to get it work. So basically i want to set amount words to check and if there is dot pastes the script. Now it actually does it if sentence is small.

<?php $orig_sentence = "What if there was m.aaa.	ultiple dotsdmy script work Im not quite sure";	function stip_news($orig_sentence) { //[FUNC]		$strip_amount_words = 250; //Example 10 words & find dot position if contains in sentence.		$sentence = implode(' ', array_slice(explode(' ', $orig_sentence), 0, $strip_amount_words));		$i = strlen($sentence);		while (0 < $i) { // [WHILE]			$characters = array("!", ".", "?");				if (substr(substr($sentence,-$i,1),-1) == $characters[0] || substr(substr($sentence,-$i,1),-1) == $characters[1] || substr(substr($sentence,-$i,1),-1) == $characters[2]) { // [CHECK]				$sentence = substr($sentence,0, -$i+1);					if ($i != 1) { // [CONTAINS DOT]						echo "<script> console.log('ORIG: " . $sentence . " Char: " . substr($sentence, -1) . " Num: " . $i . "'); </script>";						echo $sentence;						$found = 1;					}  // [CONTAINS DOT]				} // [CHECK]			if (!isset($found)) {				$found = 0;			} //[FOUND 1]			$i--;		} // [WHILE]		if ($found == 0) { // [FOUND 2]			echo $orig_sentence;		} // [FOUND 2]	} //[FUNC]stip_news($orig_sentence);?>

Actually almost done with the script, alot of lines removed & soon completed. I will release the results here after i'm finished.

 

 

Fixed code below, feel free to use & like if u do :). Only modify orig_sentence & stop_on_mark functions if needed. Also more arrays can be created to characters but remember to add them to if sentence.

<?php $space = " ";$orig_sentence = "First dot. Second dot. Third dot. First questionmark? . LOL.";	function stip_news($orig_sentence) { //[FUNC]		$stop_on_mark = 2; //Setup how many . ! ? untill sentence stop.		$strip_amount_words = strlen($orig_sentence);		$sentence = implode(' ', array_slice(explode(' ', $orig_sentence . $space), 0, $strip_amount_words));		$characters = array("!", ".", "?");		$i = strlen($sentence);		$count = 0;			while (0 < $i) { // [WHILE]			if (substr(substr($sentence,-$i,1),-1) == $characters[0] || substr(substr($sentence,-$i,1),-1) == $characters[1] || substr(substr($sentence,-$i,1),-1) == $characters[2]) { // [CHECK]				if ($count == $stop_on_mark -1) {							echo substr($sentence,0, -$i+1);							$found = 1;				} 			$count++;					} // [CHECK]			$i--;		} // [WHILE]		if (!isset($found)) { // [FOUND 2]			echo $orig_sentence;		} // [FOUND 2]	} //[FUNC]stip_news($orig_sentence);?>

Just had to wait day to get solution from my brain :)

Edited by Mudsaf
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...