tags:

views:

116

answers:

3

I am using PHP, i want to display a string which comes as title of a post, in a textbox as value. if this title has inverted commas then the string value tag of input field terminates on that quote. now as there is an add slash function to add back slashes, is there a similar function to add forward slashes ? but just before quotes, and not any other character

+5  A: 

Use the htmlspecialchars function to encode the string within the value="..." attribute.

Example:

$sometext = 'Hello "world"!';
echo '<input type="text" value="' . htmlspecialchars($sometext) . '" />';
// outputs <input type="text" value="Hello &quot;world&quot;!" />
Ricket
hey Ricket, thanx for the answer, though i got it minutes before i saw this one. one of my seniors helped me out, but the solution is same the life savior function htmlspecialchars :)
Umair
+1  A: 

As I understand, you are trying to put some text into an HTML <input type="text /> ?

If yes, you'll need to use the htmlspecialchars function ; for instance :

echo '<input type="text" name="my_element" value="'
    . htmlspecialchars($value, ENT_COMPAT, 'UTF'8) 
    . '" />';

Note that you have to specify a charset, if you are not working in ISO-8859-1.


With this function (quoting) :

  • '&' (ampersand) becomes '&amp;'
  • '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
  • ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'
Pascal MARTIN
A: 

You can use preg_replace, but what do you want to achieve with forward slashes?

preg_replace('/\"/', '/\"', $string);
Petr Peller