簡體   English   中英

如何制作用於在 Whatsapp 上共享的“文本 + 換行符 + 鏈接”?

[英]How to make a `text + line-break + link` for sharing on Whatsapp?

我在網上嘗試了很多方法來解釋這個問題,但沒有找到適合我的需要。

我想share to whatsapp我網站上每個產品的share to whatsapp鏈接,包括product name, line-break and link 像這樣的東西:

Product Name [/r/n]
https://....

我正在使用 OpenCart 3。這是 php 端代碼:

'whatsapp_text' => $result['manufacturer'] . ' - ' . $result['model'] . ' - ' . $result['name']
    . $this->encodeURIComponent('\r\n' . $this->url->link('product/product', 'product_id=' . $result['product_id']))

上面的代碼返回這個:

https://api.whatsapp.com/send?text=Nurinu%20-%201310%20-%20Bra%5Cr%5Cnhttp%3A%2F%2Fwww.myweb.com%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D61

根據此頁面( https://github.com/kriskbx/whatsapp-sharing/issues/16#issuecomment-294854157 )可以使用window.encodeURIComponent(whatsappMessage)line-break ,但我不知道如何將它與我的 php 代碼結合或在 html 端使用它:

<a href="https://api.whatsapp.com/send?text={{ product.whatsapp_text }}" data-action="share/whatsapp/share">Whatsapp</a>

更新

我忘了包括函數(encodeURIComponent):

function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}

我根據這篇文章( http://webdevelopmentscripts.com/35-share-a-link-on-whatsapp-from-website )和CBroe關於使用double quote換行"\\n"的建議解決了這個問題:

'whatsapp_text' => $result['manufacturer'] . '-' . $result['model'] . '-' . $result['name']
    . rawurlencode("\n" . $this->url->link('product/product&product_id=' . $result['product_id']))

<a href="whatsapp://send?text={{ product.whatsapp_text }}">whatsapp</a>

結果正是我想要的:

Moonslictese-251-Bra
http://www.example.com/index.php?route=product/product&product_id=46

我也可以使用encodeURIComponent

javascript:void(location.href='whatsapp://send?text='+encodeURIComponent({{ product.whatsapp_text }}))

您可以使用urlencode($yourmessage)相同。

2020 年更新

盡管這種趨勢已有多年,但我在尋找同樣的問題。 所以這是今天的當前方式:

Spaces 使用此命令: %20 (但如果在 PHP 變量中則不需要)

換行符: %0A%0D%0A (完全需要)

鏈接:不需要特殊字符

$txt_1 = 'You can see there is no need to include special commands for spaces if they  are in a PHP variable.'."%0A";
$txt_2 = 'But you do need to include some inside the variable to jump lines.'."%0D%0A";
$txt_3 = 'And nothing special for links: https://example.com';

$msg= $txt_1.$txt_2.$txt_3."%0A";

<a href="https://wa.me/put_your_number_here?text=<?php echo $msg ?>Spaces%20here%20require%20this."  target="_blank" >
//Some WhatsApp icon
</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM