Page 1 of 1
Determine if a DB field contains HTML and format correctly?
Posted: Tue May 25, 2004 8:56 am
by hairyjim
Hi,
I have a table that contains news articles, now some of the articles may contain HTML formatting while others may not.
Using standard PHP code I can retrieve the info and those that contain HTML are formatted nicely, while those that do not just appear as one big block of text with no returns.
Is it possible to determine if data being retrieved contains HTML or not and if it does then leave as is otherwise 'RAW' encode it?
I have had a trawl through the manual but could only seem to find 'Filters' which I did not think were appropriate.
Could someone point me to a suitable reference to do this?
Posted: Tue May 25, 2004 8:57 am
by malcolmboston
well you could search for obvious tags such as
<html>, <body>, <p>, <br> etc
and if you are using the nl2br at 'creation' time also search for <br />
Posted: Tue May 25, 2004 9:03 am
by hairyjim
Cheers. Smart thinking!
What are the encoding options? Encode Raw / Break for instance and where in the manual will I find the reference to use them?
Posted: Tue May 25, 2004 9:06 am
by malcolmboston
what exactly do you mean by encoding?
encoding (encrypting) data in MySQL?
HTML decleration type???

Posted: Tue May 25, 2004 9:25 am
by hairyjim
Sorry.
Say I have a field called bodytext that contains the following:
Code: Select all
<p>Some Data</p>
<p>..and some more</p>
Then using standard PHP echo"$data[bodytext]"; I would get:
This is fine, it formats the output using the HTML. Great.
Now if I were to use the same echo statement to grab the following records bodytext data that has no HTML:
I would get
Which is not so great.
Your earlier suggestion of searching for <p> is good, I will do that for it will allow me to determine how the output should be formatted to screen.
So for those records that do not contain HTML tags is there a function to output the data in another way that preserves the format 'as is' in the record? i.e with carriage returns, line breaks etc.
What is that function? Hope I make more sense now.
Posted: Tue May 25, 2004 10:49 am
by launchcode
nl2br will turn "plain text" into a format that works on an HTML page.
Searching for <p> will work fine, providing your HTML content actually has a <p> tag in it. Another way which works quite nicely (and is pretty flawless) is to take your string of data and then run strip_tags() on it (putting the result into a new string). Now compare the size of the two strings. If they are exactly the same length (strlen) then no HTML tags were removed at all. If they are not - then something happened

Posted: Tue May 25, 2004 10:55 am
by hairyjim
Ok I will check this nl2br out.