Reputation: 57
How I can remove new lines only inside the HTML tags with preg_replace ?
Example:
<table>
<tr>
<td></td>
</tr>
</table>
Text here. Text here
Text here.
So after the functions process the above code the return should be:
<table> <tr> <td></td> </tr> </table>
Text here. Text here
Text here.
Upvotes: 0
Views: 1848
Reputation: 1043
There might be smarter ways to do this, but however, this will do your job.
$str = "test\n\n test2 <table>\n\n\n test 3</table>\n\n\n test4 test5";
while ($str2 = preg_replace('/(>[^<]*)\n([^<]*<)/', '\\1\\2', $str)) {
if ($str2 == $str) break;
$str = $str2;
}
echo ($str);
It looks for newlines in between the > char and the < char, and removes them.
Upvotes: 0
Reputation: 197682
How I can remove new lines only inside the HTML tags with preg_replace ?
Technically yes, but actually, HTML does not care for newlines that much, every multiple whitespace characters are actually read as a single one. As your example shows, you replace \n with space or \t, so it's actually the same which brings me to the point you can just do the following:
$html = preg_replace('~(>[^>]*)(*BSR_ANYCRLF)\R([^<]*<)~', '$1 $3', $html);
See as well: php regex to match outside of html tags and How to replace different newline styles in PHP the smartest way?.
A more safe way is to use a HTML parser like DOMDocument
and load your fragment as body. Then replace all newlines within textnodes that are childnodes of body childnodes.
Upvotes: 2