Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Textarea problems with quotation

Hi E's, I write text in a html textarea and I save that text in my data base.
My problem is when I write something like:
<style type="text/css" media="screen">

When I save the contain of textarea the code above appear:
<style type=\"text/css\" media=\"screen\">

What php function I have to use to resolve the problem?

Regards, JC
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

you can use wordwrap function or streplace function
When you want to display the content in your DB again, just pass it through stripslashes().
$text='<style type=\"text/css\" media=\"screen\">';

wordwrap($text, strlen($text), "\", true);
$text='<style type=\"text/css\" media=\"screen\">';
 str_replace("\", "", "$text");
$text='<style type=\"text/css\" media=\"screen\">';
echo stripslashes($text);
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sidebar note... The idea at ID:37029889 is not useful, because the construct "\" will create a parse error.  The backslash is an escape character.  It has to be doubled, in effect "escaping itself" if it is to be usable in PHP.  

You can see the program in action here; it does not remove the backslash characters.  It's a good idea to test the comments and proposed solutions that you find at EE.  Not all of our experts test their programming before they post, so you may find "interesting" results if you take the answers on face value.
http://www.laprbass.com/RAY_temp_ukerandi.php
<?php // RAY_temp_ukerandi.php
error_reporting(E_ALL);

// THE EXAMPLE FROM EE
$text='<style type=\"text/css\" media=\"screen\">';

// SEE http://php.net/manual/en/function.wordwrap.php
$x = wordwrap($text, strlen($text), "\\", true); // NOTE THAT THE SINGLE BACKSLASH CAUSES A PARSE ERROR

// SHOW THE ORIGINAL AND THE OUTPUT
echo "<pre>";
echo PHP_EOL;
echo htmlentities($text);
echo PHP_EOL;
echo htmlentities($x);

Open in new window

Avatar of Pedro Chagas

ASKER

Hi Ray, I test your example and appear like this way:
<style type=\"text/css\" media=\"screen\">
<style type=\"text/css\" media=\"screen\">
Please read the post and the article at ID:37030356.  What you are testing is not "my" example.  It is the implementation of the bogus advice posted at ID:37029889.  The article explains how to handle issues caused by magic quotes.  It's worth understanding this -- magic quotes will be going away in a release of PHP that is coming to you soon, and you will want to be sure that none of your code relies on it before that release of PHP arrives.