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?

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.