I want to write a snippet of code that would delete the period at the end of sentences. However, periods that precede lowercase letters should NOT be removed. Examples of strings that should NOT remove the period * Go by 123 Abc St. and C street * e.g., here * this is a longstate... followed by Examples of strings that should have their periods removed "That's something." Bob said. He was being... As always. Don't you know? Becomes: "That's something" Bob said He was being As always Don't you know? Is there one regex I could write or should I just use a for-loop and iterate through the strings and keep track of the periods and capitalized letters? Any ideas would be greatly appreciated! Thanks!
I'd try and work out some standard rules and to a str_replace where ." becomes " and so on. Then finally check the last character, if it's a . then use substr
Thanks for your suggestion! But here is a string where I DO NOT remove the period. "That's something." like I said Notice how the letter is lowercase after the period even though there's a " after the period. Anyone else have any other ideas?
Here's an example input and output Input: "That's something." Bob said. He was being... As always. Don't you know? Output: "That's something" Bob said He was being As always Don't you know? Examples of strings that should NOT remove the period since the letter following the period is not uppercase * Go by 123 Abc St. and C street * e.g., here * this is a longstate... followed by
<?php $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?'; //remove a dot/full-stop if its followed by a capital letter. $str = preg_replace('~\. (?=[A-Z]{1})~', ' ', $str); //remove a dot/full-stop if its followed by " $str = str_replace('." ', '" ', $str); echo $str; ?> PHP:
Thanks danx10! But I notice your preg_replace doesn't replace multiple periods in the same sub string. For example: He was being... As always should be He was being As always But good job so far!
<?php $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?'; //remove a dot/full-stop if its followed by a capital letter. $str = preg_replace('~[\.]+ (?=[A-Z]{1})~', ' ', $str); //remove a dot/full-stop if its followed by " $str = str_replace('." ', '" ', $str); echo $str; ?> PHP:
How would I remove the periods before the quote? For example: Input: "That's something..." Bob said. He was being... As always. Don't you know? Like "the..." never "the..." Cat Output: "That's something" Bob said He was being As always Don't you know? Like "the..." never "the" Cat
<?php $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?'; //remove a dot/full-stop if its followed by a capital letter. $str = preg_replace('~[\.]+ (?=[A-Z]{1})~', ' ', $str); //remove a dot/full-stop if its followed by " $str = preg_replace('~[\.]+" ~', '" ', $str); echo $str; ?> PHP: