Jump to content

Recommended Posts

I've been coding with PHP for a while now, and when creating dynamic drop down boxes and more, i ALWAYS run into the problem of ' and " or other symbols being put in by users (like < > or others) and messing up the html structure.  I know you can do a str_replace with an array to swap out certain characters with an alternative (like html's  & q uot;)

 

However, I got to know, what is the best way to go about this?  I have many pages, that have to do this.  Is there a PHP function that will take text and replace any characters it can with the html &____;, or do people make up their own array and copy it onto every single page? 

 

Searched but couldn't find much.  You would think PHP would have put in a function to do this function since it seems like an issue that would come up a lot.  Thanks for any help!

 

Sincerely,

  Trevor

Seems you have a simple solution to this.

<?php
$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
?>

 

Htmlentities can also support encoding.. ($str, ENT_QUOTES, 'UTF-8')..

More here:

 

http://us2.php.net/manual/en/function.htmlentities.php

 

htmlspecialchars is more simple but doesn't HTML encode everything, which in your case may not be as wise.

 

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.