Page 1 of 2

[SOLVED] html code from database coming through as text...

Posted: Thu Mar 24, 2005 4:11 am
by batfastad
Hi

I am using PHP to manage a string of text coming out of a database.
But for some reason when I try and echo the string onto my page, the HTML code just comes through as text - so you can see the img src= and br tags.

The PHP code I'm using to manipulate the database output is basically to show only the first 180 words of the string.

Code: Select all

<small>Headline 1</small><br />
<b>Date: </b><br />
<br />
<?php
$maxlength = "180";
$newstext = addslashes("[Field: TEXT XHTML]");
$newstextnew = "";
$words = explode(" ", $newstext);

foreach ($words as $key => $word) {

	if ($key < $maxlength) {
		echo($word." ");
	} elseif ($key == $maxlength) {
		echo($word."...");
	}

}
?>
<br /><br />
<hr />
The rest of the HTML tags such as the <small> and <br /> and <hr /> that are outside of that loop work perfectly.

So I guess it must be something to do with the content-type of the string once PHP has done it's stuff.
There's nothing I can do on the database side of things to tell it text/html as the database I'm using is FileMaker Pro and the tag saying, [Field: TEXT XHTML] is a tag that gets replaced with FileMaker data, before it gets parsed by PHP.

I was wondering if there's a way to force a content type of text/html for the output of a string??
I don't really know much about mime types and content types - but I'm assuming this can only be done at the top of the page, in the headers.

Is there anyway I can tell the browser interpret each echo($word) as HTML?

Any ideas?

Thanks

Ben

Posted: Thu Mar 24, 2005 4:14 am
by Chris Corbyn
How do you insert the content into the database?

It seems like htmlentities() or htmlspecialchars() has been applied.

Does the HTML source that is output show a whole lot of > and < 's etc?

Posted: Thu Mar 24, 2005 4:20 am
by batfastad
Yep you're right!!

The data is entered into the database ok, it's just when it's output from the database.
A step that you don't really have access to in FileMaker.

Is there anyway to return them to normal, short of doing a find and replace??

Thanks

Ben

Posted: Thu Mar 24, 2005 4:21 am
by CoderGoblin
Not sure on what is happening from the details given. It sounds like the information is being stored after conversion to html entities (< becomes &lt; etc). I would recommend the following:

Code: Select all

$newstext = addslashes("[Field: TEXT XHTML]");
var_dump($newstext);
echo("<hr />");
Run the code and have a look at the resulting dumped string.

Arghh... somone beat me to it... Edited to Answer next question

You can use http://www.php.net/manual/en/function.h ... decode.php

to change &gt; to > etc. but what happens when you have &lt;p&gt;6 &lt; 7 &lt;/p&gt;

I cannot remember how this is handled but you need to test it.

Posted: Thu Mar 24, 2005 4:34 am
by batfastad
Thanks for your help guys but...

I've changed my code to this to use the html_decode_entities function at the end

Code: Select all

<?php
$maxlength = "180";
$newstext = addslashes("[FMP-Field: TEXT XHTML]");

$newstext = html_entity_decode($newstext);

$words = explode(" ", $newstext);


foreach ($words as $key => $word) {
	if ($key < $maxlength) {
		$newsdisplay .= $word." ";
	} elseif ($key == $maxlength) {
		$newsdisplay .= $word."...";
	}
}

echo(html_entity_decode($newsdisplay));

?>
But in the source it's still coming through as &gt; etc.

Any ideas?

Posted: Thu Mar 24, 2005 4:36 am
by Chris Corbyn
CoderGoblin wrote:
to change > to > etc. but what happens when you have <p>6 < 7 </p>

I cannot remember how this is handled but you need to test it.
I belive < in the original code gets converted into &lt; so that it displays in the page the way it should.... therefore this shouldn't be affected. In fact the entity decode should convert them back to normal :wink:

Posted: Thu Mar 24, 2005 4:37 am
by batfastad
In fact I've found out why it doesn't work...

the html_entities_decode function doesn't work for numeric entity references.
Guess what...

The ones coming through in my code are numeric, and not > etc.

Short of doing a find and replace for each occurence of < > and " (in fact I'd only want to replace the " when they are in between two < >), is there a way round this?

Thanks

Ben

Posted: Thu Mar 24, 2005 4:39 am
by batfastad
d11wtq wrote:
I belive < in the original code gets converted into &lt; so that it displays in the page the way it should.... therefore this shouldn't be affected. In fact the entity decode should convert them back to normal :wink:
That's well useful!!!

Posted: Thu Mar 24, 2005 4:40 am
by Chris Corbyn
You need htmlspecialchars_decode() I believe ;-)

Posted: Thu Mar 24, 2005 4:45 am
by batfastad
Now I've got this error...

Code: Select all

Fatal error: Call to undefined function: htmlspecialchars_decode()
I guess doing a replace might be the easiest way after all!

Thanks

Ben

Posted: Thu Mar 24, 2005 4:51 am
by CoderGoblin
Lets Take step back and think about this first.

Why are we getting these values? Is it because when we store the information it is incorrect or is it when the [Field: TEXT XHTML] is replaced it changes.

Would it be more effective in the long run to correct this handling rather postprocessing the output ? Adding things like html_entity_decode after the information retrieval may solve this problem but will we run into it again and have to always "patch" the result ?

Just thought I would mention this as too often we :wink: tend to "patch" input and output rather than ensure the data storage and retrieval methods are correct.

Posted: Thu Mar 24, 2005 4:55 am
by CoderGoblin
http://www.php.net/manual/en/function.h ... decode.php

Please look at the manual and see the "See also" commands. You may find them useful.

Even if this works you will need to perform testing to ensure things like <p> 6<7>3 </p> get handled correctly.

Posted: Thu Mar 24, 2005 5:39 am
by batfastad
Ok

Even if I do the filemaker tag [Field: TEXT XHTML] the HTML in the field 'TEXT XHTML' still comes through as text.
Without PHP even being involved.

But you can change this by making the tag become [Field: TEXT XHTML, Raw] then the HTML displays properly.

And that is the correct behaviour of FileMaker data.

But when you change the tag to that in the PHP code...

Code: Select all

<?php
$maxlength = "180";
$newstext = "[FMP-Field: TEXT XHTML, Raw]";

$newstext = html_entity_decode($newstext);

$words = explode(" ", $newstext);


foreach ($words as $key => $word) {
	if ($key < $maxlength) {
		$newsdisplay .= $word." ";
	} elseif ($key == $maxlength) {
		$newsdisplay .= $word."...";
	}
}

echo($newsdisplay);

?>
I get an error...

Code: Select all

Parse error: parse error, unexpected T_STRING
Any ideas how to solve this error? Or why this error is appearing when the output does come through as HTML??

Thanks

Ben

Posted: Thu Mar 24, 2005 5:52 am
by CoderGoblin
I would guess that the replacement includes a ". The line therefore gets converted to somthing like:

Code: Select all

$newstext = "<img src="img/myimage.jpg">";
This is incorrect PHP as the internal " should be \".

Don't know of an easy solution. Will think about it.

Do you still need the

Code: Select all

$newstext = html_entity_decode($newstext);
:?:

Posted: Thu Mar 24, 2005 6:06 am
by batfastad
I tried to add addslashes() to try and comment out the " marks that are part of the HTML code

Code: Select all

$maxlength = "180";
$newstext = addslashes("[FMP-Field: TEXT XHTML, Raw]");

$words = explode(" ", $newstext);


foreach ($words as $key => $word) {
	if ($key < $maxlength) {
		$newsdisplay .= $word." ";
	} elseif ($key == $maxlength) {
		$newsdisplay .= $word."...";
	}
}

echo($newsdisplay);
But I still get the T_String error.

Any help on this would be greatly appreciated!

Thanks

Ben