Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

regex

php

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.

like image 849
Pankaj Pawar Avatar asked May 13 '16 07:05

Pankaj Pawar


People also ask

How do you remove spaces from a multiline string in Python?

The string strip() method can be used to eliminate spaces from both the beginning and end of a string. Because spaces at the beginning and end of the string have been eliminated, the strip() method returns a string with just 5 characters.

Which function remove spaces at the beginning or end of a string?

The trim() method will remove both leading and trailing whitespace from a string and return the result. The original string will remain unchanged. If there is no leading or trailing whitespace to be removed, the original string is returned. Both spaces and tab characters will be removed.

How do you remove unwanted space from a string?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

What starts and ends a multiline string?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.


1 Answers

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.
like image 93
mickmackusa Avatar answered Sep 28 '22 21:09

mickmackusa