I'm making a form (html & php) which is part of an admin section used to edit content for a website. I want to allow users to include some basic html. This works fine. I want to retain line breaks. This also works. My problem is that when someone writes something like this:
<ul>
<li>item one</li>
<li>item two</li>
</ul>
the line breaks between the lines of code are retained and turned into BRs when written out. This means that there's double spacing between each LI element. Now this can be fixed by writing the whole list section on one line but a) that makes it confusing to read and b) it's hard enough teaching people to use the codes let alone explaining extraineous line breaks.
What I want is some way to strip all /n out but ONLY between UL and /UL tags.
This regular expression removes all linebreaks/whitespaces between <ul>
and </ul>
that are not part of the text between <li>
and </li>
/(?<=<ul>|<\/li>)\s*?(?=<\/ul>|<li>)/is
php example:
$output = preg_replace('/(?<=<ul>|<\/li>)\s*?(?=<\/ul>|<li>)/is', '', $input);
input:
<ul>
<li>item one</li>
<li>item two</li>
</ul>
output:
<ul><li>item one</li><li>item two</li></ul>
EDIT: fixed
You might be able to get away with using a regular expression, although this will fail if the HTML is not well formed. This should match everything within HTML tags, because the regex is greedy by default.
<?php
$str = "Hello
<ul>
<li>item one</li>
<li>item two</li>
</ul>";
$str = preg_replace_callback('~\<[^>]+\>.*\</[^>]+\>~ms','stripNewLines', $str);
function stripNewLines($match) {
return str_replace(array("", "
"), '', $match[0]);
}
echo nl2br($str);
Edit
Actually, this won't work. If there are two blocks of HTML with normal text in between, the text in the middle will also be stripped.
I'm having trouble understanding why line breaks are being turned into <BR>.
What's doing that? Is it the PHP?
Doesn't HTML treat a line break the same as a space? Turning them into <BR> doesn't seem right.
This is an example to replace line breaks in html tag:
public function testLineBreaks()
{
$html = '<span class="text"
>some title</span>';
$pattern = "#</?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/?>#";
$html = preg_replace_callback(
$pattern,
function($match){
$txt = str_replace(array("", "
"), ' ', $match[0]);
return preg_replace("/[[:blank:]]+/"," ",$txt);
},$html
);
$this->assertEquals('<span class="text" >some title</span>', $html);
}