Skip to content
Advertisement

How to give space in sentence in string with preg_replace?

Here’s i have an issue : sentence : “12223233 sekamar berempat”

$kamar = $_POST['kamarpilihan'];
 $kamarhuruf = preg_replace("/[^a-z]+""/", "", $kamar);

result: sekamarberempat

expectation: sekamar berempat

thank you

Advertisement

Answer

You can replace all chars other than lowercase letters with a space and trim the string:

$kamar = "12223233 sekamar berempat";
echo trim(preg_replace("/[^a-z]+/", " ", $kamar));
// => sekamar berempat

See the PHP demo.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement