如何在Whatsapp上分享`text + line-break + link`?

时间:2018-02-15 07:24:57

标签: javascript php hyperlink whatsapp encodeuricomponent

我尝试了许多在线解释问题的方法,但没有找到符合我需要的方法。

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

3 个答案:

答案 0 :(得分:1)

我根据本文(http://webdevelopmentscripts.com/35-share-a-link-on-whatsapp-from-website)和/(?:https?:\/\/)?(?:www\.)?(mbasic.facebook|m\.facebook|facebook|fb)\.(com|me)\/(?:(?:\w\.)*#!\/)?(?:pages\/)?(?:[\w\-\.]*\/)*([\w\-\.]*)/ig 关于使用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

答案 1 :(得分:0)

您可以将urlencode($yourmessage)用于此目的。

答案 2 :(得分:0)

更新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>