开发者

How to use urlencode in a hyperlink using phpmysql

Can you please tell me how to use urlencode in a hyperlink. I have a mysql database and it saves queries from users. This queries will be showed in my website and i want when another user click to this hyperlink it will go to google search.Because these queries contains space,i read in net that using urlencode will convert this space into 20.Below is my code.

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

This code has two problems

  1. It will open as a continuation of my website. instead of www.google.com/search?q=query, it opens as www.mysite.com/www.google.com/search?q=query

  2. Because i added urlencode in $link[link] the website opening is www.mysite.com/www.google.com/search?q=urlencode(query).

urle开发者_运维百科ncode also goes into the search term.


remove the call from the string and concatenate the result to it, that should do it

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

edit, I added some quotes around the url as well


You need to make the href attribute containing an absolute url to solve #1 and to break the string and use the concatenation for getting the url urlencoded to solve problem #2:

   echo("<br><a target=_blank href='http://www.google.com/search?q=" . urlencode($link[link]) . "' class=z>{$link['link']}</a>");
  • Third: you need to use brackets when you use an array in string interpolation
  • Fourth: you should use quotes to surround array keys to avoid notices

The following become:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜