php 包含双引号值的输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2004298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:38:11  来源:igfitidea点击:

Input field containing double quotes value

phphtml

提问by Testadmin

In my PHP project I have a value containing special characters like ",', etc. (" 5 " inches, '3.5' inches, etc.). But it does not appear in a text field. How can I display this?

在我的 PHP 项目中,我有一个包含特殊字符的值,如“、”等(“5”英寸、“3.5”英寸等)。但它没有出现在文本字段中。我该如何显示?

Is it possible to display this value in a text box?

是否可以在文本框中显示此值?

回答by Emil Vikstr?m

Use htmlentities:

使用htmlentities

<input value="<?php echo htmlentities($value);?>">

回答by Pascal MARTIN

I suppose your "text box" is an HTML <input>element?

我想你的“文本框”是一个 HTML<input>元素?

If so, you are displaying it using something like this:

如果是这样,您将使用以下内容显示它:

echo '<input name="..." value="' . $yourValue . '" />';

If it's the case, you need to escape the HTML that's contained in your variable, with htmlspecialchars:

如果是这种情况,您需要转义包含在变量中的 HTML,使用htmlspecialchars

echo '<input name="..." value="' . htmlspecialchars($yourValue) . '" />';

Note that you might have to add a couple of parameters, especially to specify the encoding your are using.

请注意,您可能需要添加几个参数,特别是要指定您正在使用的编码。


This way, considering $yourValuehas been initialized like this :


这样,考虑$yourValue已像这样初始化:

$yourValue = '5 " inches';

You'll get from this generated HTML:

你将从这个生成的 HTML 中得到:

<input name="..." value="5 " inches" />

To that one, which works much better:

对于那个,效果更好:

<input name="..." value="5 &quot; inches" />

回答by Kai Noack

For UTF-8 I went for htmlspecialchars($value, ENT_QUOTES, "UTF-8")which did the trick.

对于 UTF-8,我选择了htmlspecialchars($value, ENT_QUOTES, "UTF-8")它。

stack source

堆栈源

回答by Karl von Karton

When using character set UTF-8, I use the code below to get I?t?rnati?nàliz?ti?n anddouble (or single) quotes right:

使用字符集 UTF-8 时,我使用下面的代码来正确获取 I?t?rnati?nàliz?ti?n双(或单)引号:

<input type="text" name="title" value="<?php echo htmlentities(stripslashes(utf8_decode($title))); ?>" />

PS: This is useful after someone submitted the form, but when the input is not validated.

PS:这在有人提交表单后很有用,但是当输入未验证时。

回答by mikato

I've found if you have double quotes in a variable in JavaScript (from Ajax/database whatever) and you want to put it in a field - if you build the whole field/form HTML content and then swap that into a div using innerHTML, the double quotes in the value will cause problems. I was doing this and I couldn't figure a way around it by escaping either.

我发现如果你在 JavaScript 的变量中有双引号(来自 Ajax/数据库)并且你想把它放在一个字段中 - 如果你构建整个字段/表单 HTML 内容,然后使用 innerHTML 将它交换到一个 div ,值中的双引号会引起问题。我正在这样做,我也无法通过逃避来解决它。

You should build the HTML content with the field and swap it in first, and then do a document.getElementById('myfieldid').value = thevalue; instead and it works fine.

您应该使用该字段构建 HTML 内容并首先将其交换,然后执行 document.getElementById('myfieldid').value = thevalue; 相反,它工作正常。

回答by jza34

Personally, I use this trick:

就个人而言,我使用这个技巧:

$s = str_replace("& amp ;", "&", (htmlentities(stripslashes($s), ENT_QUOTES, 'UTF-8')));

回答by ZZ-bb

I needed to apply htmlspecialcars()to the query result array. I found a useful solution from here(see the comment by sean).

我需要申请htmlspecialcars()到查询结果数组。我从这里找到了一个有用的解决方案(请参阅肖恩的评论)。

// Create a cleaning function
function _clean(&$value) {
  $value = htmlspecialchars($value);
  //$value = htmlspecialchars($value, ENT_QUOTES); // Alternative
}

// Fetch the data from DB somehow (sqlQ is a custom function)
$q = "...";
$r = $d->sqlQ($q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);

//...call the function recursively (not always necessary) to the resultset row
array_walk_recursive($row, '_clean');

It does quite a bit unnecessary work if you fetch only a few text columns, but at least you don't need to write the htmlspecialchars()function to the HTML form multiple times.

如果您只获取几个文本列,它会做很多不必要的工作,但至少您不需要将该htmlspecialchars()函数多次写入HTML 表单。

回答by Edin

Try this

尝试这个

 echo '<input name="..." value="' . htmlspecialchars(stripslashes($yourValue)) . '" />';

or

或者

<input name="..." value="<?php echo htmlspecialchars(stripslashes($value)); ?>">

Good luck ;)

祝你好运 ;)