php 如何使用str_replace替换单引号和双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14743812/
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 07:52:30  来源:igfitidea点击:

How to use str_replace to replace single and double quotes

php

提问by Jeremy Flaugher

I have to create a login in form which a user will insert a username and a password. I have to ensure that html entities are not processed, and I can not allow single quotes or double quotes to be processed either. I must echo the data entered into the form and display it.

我必须以用户将插入用户名和密码的形式创建一个登录名。我必须确保不处理 html 实体,并且我也不允许处理单引号或双引号。我必须回显输入到表单中的数据并显示它。

I must use htmlentities and str_replace. I have the htmlentities correct, but am unsure on how to utilize the str_replace function to replace the single and double quotes that the user might enter into the form. Any help would be awesome.

我必须使用 htmlentities 和 str_replace。我的 htmlentities 是正确的,但不确定如何使用 str_replace 函数来替换用户可能输入到表单中的单引号和双引号。任何帮助都是极好的。

Here is my current PHP (which works)

这是我当前的 PHP(有效)

<?php
$username = htmlspecialchars($_POST['username']);
$password    = htmlspecialchars($_POST['password']);
$comment = htmlspecialchars($_POST['comment']);
?>
<html>
<body>
Your username is: <?php echo $username; ?><br />
Your password: <?php echo $password; ?><br />
Your Comment was: <?php echo $comment; ?>

回答by ScoRpion

I guess you want it this way.., Please check

我猜你想要这样..,请检查

$yourPostVariable = "scor\"pi'on";

//$name = str_replace(array("'", "\""), "", htmlspecialchars($yourPostVariable ) );

echo str_replace(array("'", "\"", "&quot;"), "", htmlspecialchars($yourPostVariable ) );

回答by user462990

 $keyItem = str_replace ("'","\'",$keyItem);

This replaces a single quote with an 'escaped' single quote \' .

这将用“转义”单引号 \' 替换单引号。

回答by Barmar

$username = htmlentities(str_replace(array('"', "'"), '', $_POST['username']));

回答by Samuel Liew

$username = str_replace(array("'", "\""), "", htmlspecialchars($_POST['username']));

回答by Techie

Try the below code to replace double quotes

试试下面的代码来替换双引号

str_replace(chr(34), "replace_with", $content);

For single quotes

对于单引号

str_replace("'", "replace_with", $content);

回答by rahul

$username_test = $_POST['username'];
$username = str_replace(array("'", "\"", "&quot;"), "",htmlspecialchars($username_test) );


//same for password :) 

回答by BX16Soupapes

To delete quotes, you can use this:

要删除引号,您可以使用:

$search = 'cars "black" in ...';
$search = str_replace("\"", "", $search);
// result: "cars black in ..."