【问题标题】:php variables seems to be the same but they are not identicalphp变量似乎相同,但它们并不相同
【发布时间】:2014-06-16 16:59:51
【问题描述】:

我有 2 个 php 变量:

$account_name

$my

如果我使用函数:

var_dump($account_name);

我明白了:

string(192) "admin"

$account_name 显示为帐户名称的链接

如果我使用函数:

var_dump($my);

我明白了:

string(5) "admin"

如何更改返回我唯一的“管理员”字符串的变量 $account_name

我有问题,因为如果我在 SQL 查询中使用 $account_name,则不会发生任何事情,但如果我使用 $my,它会起作用。

【问题讨论】:

  • 为什么不在查询中使用 $my?
  • 为变量赋值的代码在哪里?
  • 使用strip_tags 删除链接。

标签: php sql


【解决方案1】:

您的 $account_name 可能包含 html 标签。由于您在浏览器中查看它,它被处理为 html。您需要从这些标签中提取值。

看看this的帖子。

试试:

$account_name = strip_tags($account_name);

strip_tags reference

【讨论】:

    【解决方案2】:

    您的字符串很可能包含不可读的字符。 尝试使用此示例剥离字符串:

    var_dump(preg_replace("/[^(\x20-\x7F)]*/", '', $account_name));
    

    如果有效,它应该返回:string(5) "admin"

    【讨论】:

    • 不错的一个。但是你应该提到这仅适用于 ascii 用户名。
    【解决方案3】:

    你是如何设置变量的?

    如果你是从这样的表格中得到它:

    <form action="/" method="get">
        <input name="username" placeholder="Username">
        <button>click me</button>
    </form>
    

    您应该简单地收集输入的值并提取 php 中的值

    <?php $account_name = $_GET["username"]; echo $account_name; ?>

    在某些情况下,您可能想要剥离变量 strip($_GET["username"]);删除字符串前后的空格

    【讨论】:

      猜你喜欢
      • 2015-07-14
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多