Smough
Smough

Reputation: 212

PHP Get method, escaping the "&" value

i have this Download link that will be using a GET method, here's my code:

echo "<a href='dlexcel.php?pname=".s1."&year=".s2."'>DOWNLOAD</a>";

that will be recieve by dlexcel.php

$_GET['pname'].$_GET['year'];

the problem is, s1 is string that can contain the a value &. and the string itself is not complete when $_GET is called.

i already used str_replace and preg_replace but i dont know how to. i need to pull the & out and replace it with something else, i just dont know how or what.

Upvotes: 0

Views: 111

Answers (2)

Brad
Brad

Reputation: 163252

Try http_build_query(). http://www.php.net/http_build_query

echo '<a href="dlexcel.php?', http_build_query(array(
  'pname' => $s1,
  'year' => $s2
), '">DOWNLOAD</a>');

This takes care of encoding data, while building the entire query string from an array for you, meaning you aren't manually hacking it together. Remember, there is more than just & that you must encode.

Upvotes: 1

dave
dave

Reputation: 64657

You need to use

urlencode($s1)

when encoding a string to be used in a query part of a URL

Upvotes: 3

Related Questions