Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to output single and double quotes as a value for an input element

I have a value ($title) that is stored in MySQL and is being called, using PHP, to be inserted into the value of an input element. The problem is when a single or double quote is used, the value of the input field terminates at that point.

The behavior that should occur is the input field should be populated EXACTLY with the data in the $title variable, so that when the form is updated, the quotes remain intact.

Here is the PHP:

<?php
    echo '<input type=text size=91 name=title value="'.stripslashes($title).'">';
?>

Now, here is a typical problem: if the value of $title

this is a test " of what occurs with a quote

and I echo the variable, it echos correctly to

this is a test " of what occurs with a quote

However, when used in an input field, it renders as:

<input value="this is a test " of what occurs with a quote">

The first " terminates the value of the field, causing the new value to be:

this is a test 

I'm confused as to how to get the proper value to display and be submitted with the form, when that variable is displayed and updated.

like image 873
dihakz Avatar asked Jun 11 '13 15:06

dihakz


People also ask

Can you use single and double quotes in PHP?

Strings in PHP can be specified in four different ways: Single Quoted, Double Quoted, Heredoc Syntax and nowdac syntax. The single quoted and double quoted are the most frequently used.

How do you pass a string with double quotes?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters.

How can I use single quotes in single quotes in PHP?

Single quoted ¶ The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).


1 Answers

Try using htmlspecialchars. This will escape the " in yout title.

value="'.htmlspecialchars($title).'">
like image 61
cb0 Avatar answered Sep 17 '22 19:09

cb0