Can someone tell me how to change every character in a string EXCEPT spaces to X's in PHP? Example: "123 Hello Plaza Snottingham Hants PO12 345" would become: "XXX XXX XXXX XXXXXXXXX XXXX XXXX XXX"
<?php $string = '123 Hello Plaza Snottingham Hants PO12 345'; $regex = '/[^\s]/i'; $result = preg_replace($regex, 'X', $string); echo $result; ?> PHP:
That will also change tabs, newlines, etc., to X's which wasn't the spec. $regex = '/ /'; PHP: Though in real life I'd guess you want runs of spaces collapsed to a single X: $regex = '/ +/'; PHP: Incidentally I live in Snottingham Hants. You too?
The spec was pretty much every single character except spaces, so anything not a space should be an X I guess..