简体   繁体   English

使用PHP GET清理在href中使用的字符串

[英]sanitize string for use in href with PHP GET

I am trying to add a user-defined string to information passed to a third party via href. 我试图将用户定义的字符串添加到通过href传递给第三方的信息。 So I have something that will look like 所以我有一些看起来像的东西

<a href="http://thirdparty.com/?data_set=USERSTRING" target="_blank">Link Text</a>

USERSTRING is known when the page loads so it could be put in the href by php when the page loads, or I can dynamically add it with javascript. USERSTRING在页面加载时是已知的,因此当页面加载时可以通过php将其放入href中,或者我可以使用javascript动态添加它。

What I don't know is what I need to do to escape any special characters so that the link works and can be read on the other end - USERSTRING could be something really annoying like: [He said, "90% isn't good enough?"] The data is only used in an auto-generated file name so it doesn't need to be preserved 100%, but I'm trying to avoid gratuitous ugliness. 我不知道的是我需要做些什么来逃避任何特殊角色以便链接起作用并且可以在另一端阅读 - USERSTRING可能是非常烦人的事情:[他说,“90%不好足够吗?“]数据仅用于自动生成的文件名,因此不需要100%保留,但我试图避免无端的丑陋。

urlencode ()函数提供了您正在寻找的内容,即:

<a href="http://thirdparty.com/?data_set=<?php echo urlencode('USERSTRING'); ?>" target="_blank">Link Text</a>

You need to urlencode it. 你需要对它进行urlencode If the variant of urlencode you end up using doesn't encode '&', '#', '"', and angle brackets as it should then you'll need to HTML encode it too. 如果您最终使用的urlencode变体不编码“&”,“#”,“”和尖括号,那么您也需要对其进行HTML编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM