-1

I have code that iterates through a database using PHP, and processes the HTML to look like this:

First line etc<br />
Text blah blah blah blah, etc<br />
Moar text blah blah blah,<br />
<ul>
    <li>List Item 1</li>
    <li>List Item 2</li>
</ul>

However, I want the <br /> immediately before the <ul> removed. i.e.

...
Moar text blah blah blah,
<ul>
...

I cannot use CSS to correct this problem, as it is being parsed into a PDF format.

How do I go about removing the <br /> immediately before a <ul> (or <ol>)?

Thanks in advance

Adi
  • 5,089
  • 6
  • 33
  • 47
  • 2
    It would be very helpful if you show your PHP code – Adi Aug 02 '12 at 11:29
  • `echo $string . ($i == ($count - 1) ? '' : '
    ');` where your loop condition is `$i < $count` else if it is `$i <= $count` {`echo $string . ($i == $count ? '' : '
    ');`}
    – 19greg96 Aug 02 '12 at 11:30

2 Answers2

0

You can use regular expressions like this :

<?php

$text = 'First line etc<br />
Text blah blah blah blah, etc<br />
Moar text blah blah blah,<br />
<ul>
    <li>List Item 1</li>
    <li>List Item 2</li>
</ul>
';

$text = preg_replace('#<br\s*/>\s*<ul>#ims',"\n<ul>",$text);
Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
  • That's perfect! Exactly what I wanted. I'm not very familiar with regular expressions, and I don't have the time to learn them at the moment. – user1571215 Aug 02 '12 at 11:38
  • 1
    See the accepted answer here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Lusitanian Aug 02 '12 at 11:48
0

I use arrays for this.

$foo = get_elements_to_output_in_some_way_or_another();

echo implode("<br />\n", $foo);
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109