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
views:
116answers:
3
+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 "world"!" />
Ricket
2010-03-09 07:47:43
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
2010-03-09 07:54:41
+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'&'
'"'
(double quote) becomes'"'
when ENT_NOQUOTES is not set.'''
(single quote) becomes'''
only when ENT_QUOTES is set.'<'
(less than) becomes'<'
'>'
(greater than) becomes'>'
Pascal MARTIN
2010-03-09 07:48:25
A:
You can use preg_replace, but what do you want to achieve with forward slashes?
preg_replace('/\"/', '/\"', $string);
Petr Peller
2010-03-09 07:52:33