php remove spaces and special characters from string code example

Example 1: php strip out special characters

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Example 2: remove space from string php

<?php
$stripped = str_replace(' ', '', "10 1000 0000 000");
echo $stripped;

Example 3: remove spaces from string php

<?php
$phone = preg_replace( '/\s+/', '', "01234 567890" );
echo $phone;

Example 4: remove symbolsand spaces php

preg_replace('/[^A-Za-z0-9]/', "", $data)

Tags:

Php Example