Jump to content

htmlentities problem with a mysql database


primefalcon

Recommended Posts

Here's the issue, I have a back-end set up for my site where I talk and such.... I didn't want to use wordpress this time...

 

anyhow I have your basic HTML form take data and passing it to to a fucntion to put into a database... then when someone views the page it access the database and displays said posts I make like wordpress does but... here's my issue......

 

I also have an edit function, that pulls that data from the database and displays it in a form for editing.... my problem is this.... If I have entered any html entities such as <p> when the form pulls it, it actually reverting to the proper html tag <p> instead of keeping it as <p>, this is a problem for me since If I forget to re-enter all the htmlentities when I edit for say just a simple spelling mistake, it plays havoc on my markup...

 

For reference before I put it into the database I have it going through the mysql_real_escape function before it's entered into the database, does anyone have any idea whats going on?

Why are you entering "<p>"? Is the purpose of this data to display code to the user? If so, just use <XMP> tags so you don't have to enter in the codes for those characters.

 

Example:

<b>Here is the code</b>
<xmp>
<b>Bold Text</b>
<div>A Div</div>
</xmp>

 

Output:

 


Here is the code

<b>Bold Text</b>

<div>A Div</div>

 

I thought xmp was deprecated (in favor of pre, which does not do what I want) from 3 and absent as of html 4 let alone html5 besides xmp breaks it into new lines rather than keeping it inline, entities are what I what but when put into a textarea box, it breaks them

 

and yes it's to be able to display code to a page viewer

 

and I've tracked down the problem to when the entities gets put into the textarea tag.... what a pain

OK I have worked out a solution myself for now, I am posting it in case anyone else will find it useful

 

when pulling the data from the database to display in the textarea tag I am running a preg_match to convert it into a custom non-entity tag like so

 

$post = $row['post']; 

$patterns = array();
$patterns[0] = '/</';
$patterns[1] = '/>/';
$replacements = array();
$replacements[2] = "[lessthan]";
$replacements[1] = "[greaterthan]";
$post = preg_replace($patterns, $replacements, $post);

 

then when it gets passed from the form to the php function I have handing the form submission I am simply doing the reverse

 

$post = $_POST['post'];
$patterns = array();
$patterns[0] = '/\[lessthan\]/';
$patterns[1] = '/\[greaterthan\]/';
$replacements = array();
$replacements[2] = "<";
$replacements[1] = ">";
$post = preg_replace($patterns, $replacements, $post);

Just updating since I have found a little more elegant solution, since it shows the entities as entities

 

converting to show in the textarea

 

<?php
$post = $row['post']; 

$patterns = array();
$patterns[0] = '/</'; $patterns[1] = '/>/';
$replacements = array();
$replacements[2] = "&lt;"; $replacements[1] = "&gt;";
$post = preg_replace($patterns, $replacements, $post);
?>

 

and then converting back into a proper entity before I throw it back in the database

 

<?php
$post = $_POST['post'];

$patterns = array();
$patterns[0] = '/\&amp\;lt\;/'; $patterns[1] = '/\&amp\;gt\;/';
$replacements = array();
$replacements[2] = "<"; $replacements[1] = ">";
$post = preg_replace($patterns, $replacements, $post);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.