Pankaj Pawar
Pankaj Pawar

Reputation: 345

Remove spaces at the start of each line in a multiline string variable

I have tried to remove spaces from paragraphs, but it fails.

I have input like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et nisl nulla. Aenean interdum eget augue vehicula commodo. Etiam condimentum.

--Nunc lacinia dui eget volutpat porta. Morbi eu magna ornare, facilisis risus eget, varius sem. Proin hendrerit lacus condimentum.

--Duis eget vehicula orci. Curabitur laoreet velit sit amet ligula congue, eget consectetur risus ultricies. Curabitur dictum felis at.

I have spaces where I mention "--", I need to remove the spaces from there.

I have tried these:

str_replace(' ', '', $string)
preg_replace('/\s+/',
preg_replace('!(\<br ?/?\>)([ ]|\t)+!i', '<br />', $str);
string.replaceAll("<br />\\p{Space}+", "<br />");

Nothing works if I change \t to \s. It will remove spaces but it will also remove spaces from between words.

Upvotes: 14

Views: 4344

Answers (5)

mickmackusa
mickmackusa

Reputation: 48081

None of the currently posted answers are presenting any elegance or directness.

My regular expression contains 3 characters and a pattern modifier. /^ +/m

  • m at the end dictates that any ^ found in the pattern will represent the start of every newline (instead of the start of the string like it normally does).
  • I am using a literal blank character though some developers prefer to use \s for readability and to cover a range of whitespace characters that might be encountered.
  • + means one or more of the previous character/expression. I could have used a finite quantifier /^ {2}/m but chose pattern brevity and flexibility in this case.

Code: (Demo)

$text = <<<TEXT
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et nisl nulla. Aenean interdum eget augue vehicula commodo. Etiam condimentum.

  Nunc lacinia dui eget volutpat porta. Morbi eu magna ornare, facilisis risus eget, varius sem. Proin hendrerit lacus condimentum.

  Duis eget vehicula orci. Curabitur laoreet velit sit amet ligula congue, eget consectetur risus ultricies. Curabitur dictum felis at.
TEXT;

echo preg_replace('/^ +/m', '', $text);

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et nisl nulla. Aenean interdum eget augue vehicula commodo. Etiam condimentum.

Nunc lacinia dui eget volutpat porta. Morbi eu magna ornare, facilisis risus eget, varius sem. Proin hendrerit lacus condimentum.

Duis eget vehicula orci. Curabitur laoreet velit sit amet ligula congue, eget consectetur risus ultricies. Curabitur dictum felis at.

Upvotes: 20

albert.garipov
albert.garipov

Reputation: 473

join("\n", array_map('trim', preg_split('/\R/', $string)));

Upvotes: 1

sfy
sfy

Reputation: 3278

$str = "REPlACE THIS WITH YOUR TEXT STRING";
// split the string by the newline character, no limit, without empty
$_arr = preg_split("/[\r\n]+/",$str,-1,PREG_SPLIT_NO_EMPTY);

$arr=[];
foreach($_arr as $line){
    // trim these line
    array_push($arr,trim($line));
}

var_dump($arr);

I have tested, works.

Upvotes: 1

Majid Abbasi
Majid Abbasi

Reputation: 1531

you can use from trim like this:

ltrim($string, ' ');

this code will remove first space from $string, also there are rtrim, trim functions.

Upvotes: -1

Webeng
Webeng

Reputation: 7143

//separates the paragraph into 3 strings, divided by '--'
  list($string1, $string2, $string3) = explode('--',$string);

//trim() takes out the spaces before and after each string
  $string1 = trim($string1);
  $string2 = trim($string2);
  $string3 = trim($string3);

  echo $string1." ".$string2." ".$string3;

Let me know if that worked for you.

Upvotes: -1

Related Questions