【问题标题】:php - how to prevent echo &macr creates ¯ character,php - 如何防止 echo &macr 创建¯ 字符,
【发布时间】:2019-11-19 13:11:15
【问题描述】:

我想回显一个 url 变量的字符串:

echo "direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html";

除了 '&macr' 创建 ¯ 字符。如何防止这种情况发生?

【问题讨论】:

  • echo htmlspecialchars($your_string);
  • " 也会导致 T in Test 也被转义。要么在你的反斜杠上加倍,要么改用'
  • 既然你已经声明字符串是 URL 参数,你可能需要rawurlencode() 而不是htmlspecialchars() - 这取决于你用它做什么。

标签: php html string url character


【解决方案1】:

htmlspecialchars 函数用于将特殊字符转换为 HTML 实体。请参阅,https://www.php.net/manual/en/function.htmlspecialchars.php

您可以使用此代码:

$str = direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html
echo htmlspecialchars($str);

或者你可以使用正则表达式:

echo preg_replace('/[^a-zA-Z0-9_ -]/s','', $str);

【讨论】:

    【解决方案2】:

    如果您想将字符串打印为原始文本(忽略任何 html 特殊实体或标签),请使用函数 htmlspecialchars($string)

    例子:

    echo htmlspecialchars($your_string);
    

    详情:https://www.php.net/manual/en/function.htmlspecialchars.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 2014-08-13
      • 2021-06-12
      • 2012-10-02
      相关资源
      最近更新 更多