I have a something like the following strings:

accounts.paul.12.7.20.5.payment.received
accounts.john 1.7.6.31.payment
management.42.1.382.33.request.denied

Notice how every space has a dot

I'd like it returned like this:

accounts paul 12.7.20.5 payment received
accounts john 1.7.6.31 payment
management 42.1.382.33 request denied

Basically I want all the dots removed from strings except the dots that exist between numbers. I'm just learning regex and really having trouble getting this. I can do it if the dots appear in the same position using strrpos and substr but not when they appear in different locations like above which is why I need help.

Any help would be greatly appreciated.

    There may be a more elegant way, but this seems to work:

    <?php
    $test = <<<EOD
    accounts.paul.12.7.20.5.payment.received
    accounts.john 1.7.6.31.payment
    management.42.1.382.33.request.denied
    EOD;
    
    $regexp = array(
       '#(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})#',
       '#\.#',
       '#<<<dot>>>#'
    );
    $replace = array(
       '\1<<<dot>>>\2<<<dot>>>\3<<<dot>>>\4',
       ' ',
       '.'
    );
    
    $test = preg_replace($regexp, $replace, $test);
    echo "<pre>$test</pre>";
    

      Thanks very much for your time NogDog. If I'm honest I don't fully understand it all but it does work. I've also never come across <<<EOD either and have looked that up too and understand that now so learned more than I expected http://php.net/manual/en/language.types.string.php

      Thanks again for you time.

        The "big picture" is that by using an array of search regexp's and a corresponding array of replacements, the preg_replace() ends up doing multiple passes, first searching/replace with the first element of each array, and so on. That way you can convert the IP addresses into something that won't be affected by the next step, which changes all dots to spaces, then the final step turning the arbitrarily chosen "<<<dot>>>" place-holders into dots again.

          Write a Reply...