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.
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.
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.
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.
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.
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).\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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With