Eliminate white space in between characters in PHP [duplicate]

Just use preg_replace (select all spaces (which comes more than one in a row) and replace it with a single space).

Example:

<?php
$str = "    i have       something       here, please allow      me!    ";
echo trim(preg_replace('/\s{2,}/', ' ', $str));

Leave a Comment