PHP - is there a better way to do this?

PHP - is there a better way to do this?

Author
Discussion

TonyRPH

Original Poster:

12,977 posts

169 months

Monday 27th March 2023
quotequote all
My (self written) PHP site reads pages from a database.

I want to insert <br> and/or <p></p> tags however using nl2br results in w3c validation fail, because all table tags for example are punctuated with <br>

I did initially write a function to ignore a block of text between two fake tags e.g. <!--start-->(.+)<!--finish> however I couldn't get this to work with multiple start/finish tags in the same page.

So I came up with This solution (pastebin link)

However it feels wrong doing it like this, so is there a better way to do it?

TonyRPH

Original Poster:

12,977 posts

169 months

Monday 27th March 2023
quotequote all
The table is just plain text in the database.

It's stored like this snippet; So apart from writing it all on one long line, I can't think how to store it without line breaks?

I could of course remove all the line breaks when reading it out of the db however that'll affect all the plain text as well.

Alternately, I could store it without line breaks, but that would make any (direct in db) incidental edits fun. And I would still need to restore the line breaks for text paragraphs.

text said:
<table class="center">
<tr>
<th>OPAMP</th><th>THD</th><th>Notes</th>
</tr>
<tr>
<td>LM4562NA</td><td>0.00027%</td><td></td>
</tr>
</table>

TonyRPH

Original Poster:

12,977 posts

169 months

Monday 27th March 2023
quotequote all
mw88 said:
Not the answer you're looking for but if the site works and looks correct in all major browsers I'd ignore the W3C Validator personally! Can't remember the last time I checked a site using it.
Yes the site functions perfectly in all the browsers I tested. I'm just a bit of a perfectionist.

king arthur said:
<stuff>
Ah I see what you mean now.

The pages stored in the db are mixed plain text but where there are links, images or tables etc. then of course it's html.

I guess I could create a separate database table for the tables I have (one of my other sites works like this, but that is solely tabular information).

Rather than apply <br> tags to everything during editing I'll just stick with my kludge. Performance doesn't seem to suffer so I'm happy with that.




TonyRPH

Original Poster:

12,977 posts

169 months

Monday 27th March 2023
quotequote all
budgie smuggler said:
Then check it for tags and only nl2br when there are none?
That's what my solution (pastebin link) is doing however I thought there might be a better way.

TonyRPH

Original Poster:

12,977 posts

169 months

Tuesday 28th March 2023
quotequote all
king arthur said:
If you know that your plain text part never has any angle brackets at the end of a line in it then you could do something like

$string = str_replace(">\r\n", ">", $string);

That would remove the line breaks after all opening and closing tags where you don't want a <br> to be inserted.

Then do the $string = nl2br($string). Should work.

Edited by king arthur on Monday 27th March 14:41
Thanks I tried this but as expected it caught too many tags, so I ended up with this (which is just a variant on my original idea).

This still catches "<table class="blah">" but I can live with that for now, and I'll format the tables some other way, or create specific database tables and use a for loop to read them out - or perhaps just use pure CSS tables (one of my pages already has this).

code said:
function nl2pY($string) {
$ends_with = array("<table>", "</div>\n", "</th>\n", "<tr>", "</tr>\n", "</td>\n");
$stuff = '';
$lines=explode("\n", $string);
foreach ($lines as $line) {
$Xline = str_replace(array("\r\n", "\r", "\n"), "\n", $line);
$match = (str_replace($ends_with, '', $Xline) != $Xline);
if ($match == "1") {
$stuff .= "{$Xline}";
} else {
$stuff .= "{$Xline}<br>\n";
}
}
return $stuff;

TonyRPH

Original Poster:

12,977 posts

169 months

Tuesday 28th March 2023
quotequote all
king arthur said:
<snip>

But really, you should not be relying on <br> tags to space out <p> and <div> elements, you should use CSS to create the margins and padding you want, <br> should only be for blocks of text in a paragraph.
The site (in my profile) is not that sophisticated, and largely consists of a few pages of text, some with tables and images and some links.

It's just a hobby site, I'm not a web dev (that much is evident!) - however I am proud to say that it is written from the ground up and doesn't depend on any CMS.

All the CSS is done by hand, no bootstrap etc. and no PHP frameworks.




TonyRPH

Original Poster:

12,977 posts

169 months

Tuesday 28th March 2023
quotequote all
Well, that was a good day's work.

All tables are now stored in the database (as SQL tables and not HTML) and are dynamically created when read out, so no more issues with tags.

Link to Pastebin code (dynamic table generation)