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.
remove new lines only inside the HTML tags
: That's really innovative, why do you want to do this - anubhava 2012-04-05 22:21
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.
Warning: preg_replace() [function.preg-replace]: Compilation failed: (*VERB) not recognized at offset 13 i - BetterMan21 2012-04-05 22:51
u
modifier instead in case the string is utf-8 encoded - hakre 2012-04-05 23:06
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.
Test test test test test. Test test test test test.
Test test test test test.
<ul>
tag - which you have asked for - hakre 2012-04-05 23:08
DOMDocument
. Just using regexp, that would be a (close to) impossible task - Alfred Godoy 2012-04-05 23:16