How to keep textarea from converting special chars

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

PeterMartin
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 08, 2011 12:05 pm

How to keep textarea from converting special chars

Postby PeterMartin » Fri Apr 08, 2011 1:00 pm

Not sure if this is HTML or PHP related.

Code: Select all

$content = "&foobar";
<textarea rows="25" cols="70" name="content"><?php echo $content; ?></textarea>


textarea then displays &foobar

I'm using this to edit code files and this is not what I want. How can I get it to NOT make the conversion?

Thanks. :help:

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: How to keep textarea from converting special chars

Postby egami » Mon Apr 11, 2011 9:11 am

so you want the textfield to display the "&amp;foobar" and not "&foobar" ?

I believe it would be in the htmlentities? or htmlspecialchars?

PeterMartin
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 08, 2011 12:05 pm

Re: How to keep textarea from converting special chars

Postby PeterMartin » Mon Apr 11, 2011 10:12 am

Thanks. Yes, I want it to display &amp; as that is what is in the file from which it gets its input.

I'm just learning PHP and simply cannot figure this out. Here's the relevant code:

Code: Select all

if (isset($_POST['content'])) {
   $content = stripslashes($_POST['content']);   
   $fh = fopen($fspec,"w") or die ("Error opening file in write mode");
           fputs($fh,$content);
           fclose($fh) or die ("Error closing file");
   echo "Saved";
}
?>

<form action="<?php echo $_SERVER["PHP_SELF"].'?q='.base64_encode($fspec); ?>" method="post">
   <div><textarea rows="25" cols="75" name="content"><?php htmlspecialchars(readfile($fspec)); ?></textarea></div>
   <div><input type="submit" value="Save" /></div>
</form>


Basically, I just want to display in textarea EXACTLY what is in the file, and save it EXACTLY as entered there...with no conversions taking place anywhere.

I think I've tried all combinations of htmlspecialchars in both reading and writing the file, and it keeps making conversions somewhere. If I use it during the file write, it converts the < and > which I do not want.

Grrrrr...I must be missing something obvious. :(

Basically, I just want to display in textarea EXACTLY what is in the file, and save it EXACTLY as entered there...with no conversions taking place anywhere.

vgould
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Apr 29, 2011 8:05 am

Re: How to keep textarea from converting special chars

Postby vgould » Fri Apr 29, 2011 8:37 am

this will work for you:
<?php echo htmlentities("&amp;foobar"); ?>

PeterMartin
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 08, 2011 12:05 pm

Re: How to keep textarea from converting special chars

Postby PeterMartin » Sat Apr 30, 2011 9:05 am

vgould wrote:this will work for you:
<?php echo htmlentities("&amp;foobar"); ?>


Thank you very much. That was it! :)


Return to “HTML Basics”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron