javascript - 如何制作 `text + line-break + link` 以在 Whatsapp 上分享?

标签 javascript php hyperlink whatsapp encodeuricomponent

我在网上尝试了很多方法来解释这个问题,但没有找到适合我需要的方法。

我想在我的网站上为每个产品制作一个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/

相关文章:

javascript - 使用 jQuery 获取 dt 的数量

php - 匹配节点id和mysql表id

php - 查询检索30天内更新的记录在mysql中无法正常工作

ios - 深层链接 Facebook iOS - 应用程序打不开

python - 在 Python virtualenv 中导入自己的 C 模块

javascript - 具有 mui-datatables 的对象数组

javascript - LocalStorage - 通过表单保存名称 - 在其他页面上显示

javascript - Angular 2 绑定(bind)到计算 getter 会出现调试错误

PHP MySql 双引号比单引号

javascript - 在jquery中做效果后转到链接