Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove newline character from a digit in the string using PHP regular expression

How can I remove a new line character in front of every digit from a string using PHP regular expression?

Example:

$message = "
My Number is \n
0\n
7\n
8\n
9\n
Come Home\n
"

Becomes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$message = "
My number is\n
0789\n
Come Home\n
"

This is what I have but it delete the everything

$message = trim(preg_replace('\d+\s+', '', $message),'\n');

>Solution :

You may use this php code:

$message = preg_replace('/(?<=\d)\R+(?=\d)/', '', $message);

This will match 1+ of any line breaks if it is followed and preceded by a digit.

RegEx Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading