[英]urlencode php example
I want to pass a php variable as part of the link. 我想将php变量作为链接的一部分传递。 How can I achieve this?
我该如何实现?
$x = 24; //$x is a php variable
How to pass this value as part of a href="". 如何将此值作为href =“”的一部分传递。 A working example would be helpful.
一个有效的例子将是有帮助的。
<A href="whatever.php?x=<php echo $x;?>">link</a>
It works mostly like the version you know from Javascript: 它的工作方式与您从Javascript知道的版本非常相似:
print '<a href="page.php?x=' . urlencode($x) . '&y=2">click here</a>';
Or if you want to use the PHP inline html notation style (with short tags <?=
enabled): 或者,如果您想使用PHP内联html表示法样式(启用短标签
<?=
启用):
<p><a href="page.php?x=<?= urlencode($x) ?>">click here</a></p>
$x = 24;
$y = 'some text';
print('<a href="http://site.com/url.php?x=' . rawurlencode($x) . '&y=' . rawurlencode($y) . '"></a>');
in url.php file you can get this variables using 在url.php文件中,您可以使用
$x = $_GET['x'];
$y = $_GET['y'];
Use http_build_query()
to build query string ( a=b&c=d
). 使用
http_build_query()
构建查询字符串( a=b&c=d
)。 This function handles all typical probles like escaping (eg. #
→ %23
) or proper HTML mark-up ( &
→ &
) etc. 此函数处理所有常见问题,例如转义(例如
#
→ %23
)或正确的HTML标记( &
→ &
)等。
printf('<a href="http://site.com/url.php?%s">Click me</a>', http_build_query(array(
'a' => $x,
'b' => 'Some strange characters like #$^$%&%^*^&('
)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.