php 引用条

发布于 2024-07-08 01:01:11 字数 251 浏览 6 评论 0原文

在我的网页中,我希望网站向用户打招呼,但用户名被“单引号”包围。 因为这并不是为了防止 MySQL 注入,所以我只是想删除显示页面上我的名字周围的引号。

例如:欢迎“用户”! 我正在尝试找到一种方法,可以删除用户周围的引号并将其显示在下面的示例中。

例如:欢迎用户!

我认为相关的唯一代码行是:

$login = $_SESSION['login'];

有谁知道如何去掉单行引号?

In my webpage, I want the website to greet the user, but the username is surrounded by 'single quotations'. Since this isn't to prevent MySQL injection, i just want to remove quotes around my name on the display page.

Ex: Welcome 'user'!
I'm trying to find the way where i can strip the quotations around the user and have it display on the example below.

Ex: Welcome user!

The only line of code that I can think relating is this:

$login = $_SESSION['login'];

Does anyone know how to strip single lines quotes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-07-15 01:01:12

如果您确定 $login 的第一个和最后一个字符始终是 ' 您可以使用 substr() 执行类似的操作

$login = substr($_SESSION['login'], 1, -1); // example 1

您可以使用以下命令从字符串中删除所有 ' str_replace()

$login = str_replace("'", '', $_SESSION['login']); // example 2

或者你可以使用 trim() 函数,该函数事实上与示例 1 相同:

$login = trim($_SESSION['login'], "'"); // example 3

我个人最喜欢的是示例 3,因为它可以轻松扩展以去除两种引用类型:

$login = trim($_SESSION['login'], "'\""); // example 4

If you're sure that the first and last characters of $login are always a ' you can use substr() to do something like

$login = substr($_SESSION['login'], 1, -1); // example 1

You can strip all ' from the string with str_replace()

$login = str_replace("'", '', $_SESSION['login']); // example 2

Or you can use the trim() function, which is in fact the same as example 1:

$login = trim($_SESSION['login'], "'"); // example 3

My personal favorite is example 3, because it can easily be extended to strip away both quote types:

$login = trim($_SESSION['login'], "'\""); // example 4
做个ˇ局外人 2024-07-15 01:01:12

我认为最简单的方法是使用 trim() 函数。 它通常会修剪空白字符,但您可以向它传递一个包含要删除的字符的字符串:

echo 'Welcome ' . trim($login, "'");

请参阅 http://php.ini。净/修剪

I think the easiest way would be to use the trim() function. It usually trims whitespace characters, but you may pass it a string containing characters you want to be removed:

echo 'Welcome ' . trim($login, "'");

See http://php.net/trim

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文