2014-10-12 20 views
0

我正在使用Wordpress插件,它向用戶發送通知電子郵件。在這樣的電子郵件中,我知道要嵌入一個ahref鏈接,它將某些變量作爲查詢參數傳遞。如何正確地將變量追加到鏈接作爲查詢?

目前我使用兩個變量在我的插件:

$email (the email address of the user) 
$scheduler->post (the name of the post, from which a user has activated the notification plugin) 

我現在想創建一個AHREF鏈接,追加兩個變量的URL,這樣我就可以在表單的用戶訪問他們正在引導到點擊鏈接時。

的目的是創造這樣的URL:

www.website.com/[email protected]&postname=example 

我怎樣才能做到這一點?該網址應該如何顯示?

是否這樣?

<a href="http://www.example.com/?email=$email&postname=$scheduler->post">Visit this link</a> 

感謝您的幫助。

亞歷山大

回答

0

你應該在添加到URL之前轉義變量。 PHP包括http_build_query()功能將建立使用變量的查詢字符串在陣列中,例如使用:

$email = '[email protected]'; 
$postname = 'postname'; 
$url = 'http://www.example.com/'; 

$queryString = http_build_query(array(
    'email' => $email, 
    'postname' => $postname 
)); 

$link = '<a href="' . $url . . '?' . $queryString . '">Visit this link</a>'; 

另見PHP的urlencode()功能其可用於代替http_build_query()

+0

'urlencode()'做了詭計。謝謝! – Alex 2014-10-14 11:34:43

相關問題