如何使用php mysql在超链接中使用urlencode

时间:2011-08-23 13:33:29

标签: php hyperlink

您能否告诉我如何在超链接中使用urlencode。我有一个mysql数据库,它保存用户的查询。这个查询将在我的网站上显示,我想当另一个用户点击这个超链接它将进入谷歌搜索。因为这些查询包含空格,我在网上读到使用urlencode将此空间转换为20.Below是我的代码。

echo("<br><a target=_blank href=www.google.com/search?q=urlencode($link[link]) class=z>$link[link]</a>");

此代码有两个问题

  1. 它将作为我网站的延续开放。 而不是www.google.com/search?q=query, 它以www.mysite.com/www.google.com/search?q=query

  2. 打开
  3. 因为我在$link[link]中添加了urlencode,网站的开头是www.mysite.com/www.google.com/search?q=urlencode(query)

  4. urlencode也会进入搜索字词。

2 个答案:

答案 0 :(得分:1)

从字符串中删除调用并将结果连接到它,应该这样做

echo("<br><a target=_blank href='www.google.com/search?q=".urlencode($link[link])."' class=z>$link[link]</a>");

编辑,我在网址周围添加了一些引号

答案 1 :(得分:1)

你需要制作包含绝对网址的href属性来解决#1并打破字符串并使用连接来获取urlenclen来解决问题#2:

   echo("<br><a target=_blank href='http://www.google.com/search?q=" . urlencode($link[link]) . "' class=z>{$link['link']}</a>");
  • 第三:在字符串插值中使用数组时需要使用括号
  • 第四:你应该使用引号括起数组键以避免通知

以下成为:

class=z>$link[link]</a>     # this is wrong
class=z>{$link['link']}</a> # should be this

http://php.net/manual/en/language.types.string.php