Php replace regular expression

I need to use php to add a space between the period and the next word / letter when not there.

For example, "This is a sentence. This is the next." should become "This is the sentence. This is the next." Pay attention to the added space after the first period.

My problem is that even if I can make a regular expression that finds every dot followed by a letter, how can I replace that dot with “dot + space” and save the letter?

It is also necessary to keep the case of letters lower or higher.

Thanks for your input.

+4
source share
3 answers
$regex = '#\.(\w)#'; $string = preg_replace($regex, '. \1', $string); 

If you want to capture more than just periods, you can do:

 preg_replace('#(\.|,|\?|!)(\w)#', '\1 \2', $string); 

Just add the characters you want to replace in the first () block. Remember to avoid special characters ( http://us.php.net/manual/en/regexp.reference.meta.php )

+9
source
 $str = "Will you please slow down?You're gonna kill someone.Seriously!"; echo preg_replace('/(!|\?|\.)([^\s\.\?!])/', '\1 \2', $str); 
+1
source
  $ str = "This is a sentence.This is the next one.";
 echo preg_replace ("# \. (\ S) #", '. \ 1', $ str);
0
source

Source: https://habr.com/ru/post/1310245/


All Articles