我在网上尝试了很多方法来解释这个问题,但没有找到适合我需要的方法。
我想在我的网站上为每个产品制作一个share to whatsapp
链接,包括产品名称、换行符和链接
。像这样:
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)
进行 换行
,但我不知道如何将它与我的 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);
}
最佳答案
2020 年更新
尽管这种趋势已经存在多年,但寻找与我在这里遇到的相同问题。所以这是今天的当前方法:
Spaces uses this command: %20 (but not necessary if inside a PHP variable)
Line breaks: %0A or %0D%0A (Totally required)
Links: No special character needed
$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>
关于javascript - 如何制作 `text + line-break + link` 以在 Whatsapp 上分享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48801892/