How can I remove all spaces from the beginning and end of each line using regex? [duplicate]

In the client-side, the user will write his bio and pass it to the API. Now, I expect that when the user writes his bio, I want to store it in the database like this:

Vivamus feugiat orci sed posuere sodales. Praesent in tincidunt ligula.

Sed ac nisl ipsum. Phasellus et ipsum quis metus blandit vestibulum. Praesent et ligula sodales, ornare lectus in, pellentesque orci.

Phasellus accumsan tristique libero, bibendum convallis augue dapibus vel.

As you can see, there are no multiple white spaces in the bio, only one white space is allowed.

Now, I receive the bio from the client-side like this: (regex101.com)

enter image description here

As you can see, there are multiple white spaces in this bio, and these multiple white spaces are unnecessary.

To clean unnecessary white spaces, I used trim() to remove all white spaces from the beginning and end of the string. However, that is not enough. I also want to remove all spaces from the beginning and end of each line, and here is what I tried:

<?php
echo preg_replace("/(?:^( )+)|(?:( )+$)/m", "$1$2", trim($_POST["bio"]));

Now the question is, is it possible to change $1 and $2 to nothing, like this -> "", instead of becoming -> " "?

You may suggest replacing the second parameter of preg_replace from "$1$2" to "". However, I don’t want this solution because I still need to play with regex later to remove other multiple white spaces such as ntrv and replace them with one.

So, let’s go back to my question. I need to tell the regex that when I say $1 and $2, I want it to replace with "" and not with " ". Is it possible?

Note: I hope you don’t close this question just because I left an image instead of text. I tried this, but it is hard to make it readable for users. Therefore, I included a URL of regex on regex101.com.