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?
Determine if a DB field contains HTML and format correctly?
Moderator: General Moderators
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
Sorry.
Say I have a field called bodytext that contains the following:
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.
Say I have a field called bodytext that contains the following:
Code: Select all
<p>Some Data</p>
<p>..and some more</p>Code: Select all
Some Data
..and some moreNow 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 moreCode: Select all
Some Data..and some moreYour 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.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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
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