Sanitize string for use in href with PHP GET

I am trying to add a user-defined string to the information passed to a third party via 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 can be placed in href by php when the page loads, or I can add it dynamically using javascript.

I don’t know what I need to do to avoid any special characters so that the link works and can be read on the other end. USERSTRING can be something really annoying, for example: [He said: β€œ90% isn't good enough?]] The data is used only in the auto-generated file name, so it does not need to be saved 100%, but I try to avoid free ugliness.

+4
source share
2 answers

The urlencode () function provides exactly what you are looking for, i.e.:

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

You need urlencode it. If the urlencode option that you end up using does not encode "&", "#", "and" brackets ", as you should, you will also need to encode HTML.

+2
source

All Articles