Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove new lines from string in PHP
To remove newlines from the string, the code is as follows−
Example
<?php
$str = "Demo
text for
reference";
echo nl2br($str);
$str = preg_replace('~[\r
]+~','', $str);
echo "
".nl2br($str);
?>
Output
This will produce the following output−
Demo<br /> <br /> text for <br /> <br /> reference Demo text for reference
Example
Let us now see another example −
<?php
$str = "Demo
text";
echo nl2br($str);
$str = str_replace(array("
", "\r"), '', $str);
echo "
".nl2br($str);
?>
Output
This will produce the following output−
Demo<br /> <br /> text Demo text
Advertisements