- var_dump: documentation ( source)
- preg_replace: documentation ( source)
<?php
$textbefore = "text has newlines in wo\nrds and normal newlines \n bewtween words and again in wo\nrds";
$textafter = preg_replace( "/(?<=\S)\n|\n(?=\S)/", '', $textbefore);
var_dump( $textafter);