简体   繁体   中英

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 . Something like this:

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

I'm using OpenCart 3. Here is php side code:

'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:

<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):

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" :

'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 :

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

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

Update 2020

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)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM