[英]How to make a `text + line-break + link` for sharing on Whatsapp?
I tried many approaches online explaining the issue, but didn't find the one could fit with my need.我在网上尝试了很多方法来解释这个问题,但没有找到适合我的需要。
I want to make a share to whatsapp
link on my website for each product, including product name, line-break and link
.我想share to whatsapp
我网站上每个产品的share to whatsapp
链接,包括product name, line-break and link
。 Something like this:像这样的东西:
Product Name [/r/n]
https://....
I'm using OpenCart 3. Here is php side code:我正在使用 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']))
Above code returns this:上面的代码返回这个:
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
According to this page ( https://github.com/kriskbx/whatsapp-sharing/issues/16#issuecomment-294854157 ) it's possible to use window.encodeURIComponent(whatsappMessage)
to have a line-break
, but I don't know how to combine it with my php code or use it in html side:根据此页面( 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>
UPDATE更新
I forgot to include the function (encodeURIComponent):我忘了包括函数(encodeURIComponent):
function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
I fixed the issue according to this article ( http://webdevelopmentscripts.com/35-share-a-link-on-whatsapp-from-website ) and CBroe
's suggestion on using double quote
for line break "\\n"
:我根据这篇文章( 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>
The result is exactly what I want:结果正是我想要的:
Moonslictese-251-Bra
http://www.example.com/index.php?route=product/product&product_id=46
Also I could use encodeURIComponent
:我也可以使用encodeURIComponent
:
javascript:void(location.href='whatsapp://send?text='+encodeURIComponent({{ product.whatsapp_text }}))
您可以使用urlencode($yourmessage)
相同。
Even though this trend has its years, looking for the same question I got here.尽管这种趋势已有多年,但我在寻找同样的问题。 So this is for today a current way to go:所以这是今天的当前方式:
Spaces uses this command: %20 (but not necessary if inside a PHP variable) Spaces 使用此命令: %20 (但如果在 PHP 变量中则不需要)
Line breaks: %0A or %0D%0A (Totally required)换行符: %0A或%0D%0A (完全需要)
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.