Search code examples
phpregexpreg-replace

Regex / Setting to much dots between lowercase and uppercase Words


i have some trouble with a Regex, the purpose of this script is to do a Auto Corrrection of some Words

Example which he should Match and Replace are:

Word.Word

To: Word. Word

wordWord

To: word. Word

I do this with the following script

function filter_string($string) {
    $string = preg_replace("/([a-z])([A-Z])/", "$1. $2", $string);
    $string = preg_replace("/([a-z]).([A-Z])/", "$1. $2", $string);
    $string = str_replace("...", "&hellip", $string);
    $string = str_replace("Games. Com", "GamesCom", $string);
    $string = str_replace("Pv. P", "PvP", $string);
    $string = str_replace("Pv. E", "PvE", $string);
    $string = str_replace("Blizz. Con", "BlizzCon", $string);
    $string = str_replace(". PvP", " PvP", $string);
    $string = str_replace(". PvE", " PvE", $string);
    return $string;

But for some reason he is matching also

Legendary Pictures zeigt uns das offizielle Poster zum Warcraft Film.

And replace it to

Legendary. Pictures zeigt uns das offizielle. Poster zum. Warcraft. Film. 

I don't get where all the dots are coming from and why he is Matching this, i hope that someone could give me a hint whats wrong with it.


Solution

  • replace your second regex:

    $string = preg_replace("/([a-z]).([A-Z])/", "$1. $2", $string);
    

    with this one:

    $string = preg_replace("/\.(\w)/", ". $1", $string);
    

    You probably forgot to escape the dot (its a token for any character).