Determine if a DB field contains HTML and format correctly?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Determine if a DB field contains HTML and format correctly?

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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 />
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

what exactly do you mean by encoding?

encoding (encrypting) data in MySQL?
HTML decleration type???

:? :? :?
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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:

Code: Select all

Some Data

..and some more
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:

Code: Select all

Some Data
..and some more
I would get

Code: Select all

Some Data..and some more
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.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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 :)
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Ok I will check this nl2br out.
Post Reply