0

Hi today I have a string that can come with duplicate spaces and strings currently I'm using the code below to clean up

<?php
$string = '    lorem ipsum dolor s            it amet content     ';
trim( $string );
ACM
  • 38
  • 4

1 Answers1

1

You can use the code below it clears the spaces on the sides and also removes line breaks

<?php
    function remove_space( $text ) {
        $text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
        $text = preg_replace('/([\s])\1+/', ' ', $text);
        $text = trim( $text );
        return $text;
    }

    $exemplo = ' Lorem ipsum dolor sit      amet quaqluer dado
    test content generates              alrel lrea   ';
    $new_string = remove_space( $exemplo );

    echo $new_string; 
Kelvin Mariano
  • 991
  • 8
  • 15