How to make a `text + line-break + link` for sharing on Whatsapp?
Asked Answered
M

4

5

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);
}
Moises answered 15/2, 2018 at 7:24 Comment(8)
'\r\n' - that’s not a line break.Eskimoaleut
@CBroe: So what should I use instead?Moises
Your knowledge of the PHP syntax basics …? php.net/manual/en/language.types.string.phpEskimoaleut
Here is the output using rawurlencode: Here is the output: Moonlight-263-Bra%5Cnhttp%3A%2F%2Fwww.mysite.com%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D55 Any idea how to make a line break?Moises
Read the link to the manual that I posted ... and pay attention to the difference between strings enclosed in single, or in double quotes ...Eskimoaleut
I think I got the point, but unfortunately still can't fix the issue. I'll be really thankful if you write an answer. I tried this: 'whatsapp_text' => $result['manufacturer'] . urlencode("\n" . $this->url->link('product/product', 'product_id=' . $result['product_id']))Moises
The link you referred to is talking about using whatsapp://send?text=, which is something different than what you are using here. Not sure whether what you want is even possible using the latter; tried a few combinations of differently encoded line breaks, and they were either replaced with simple spaces, or whatsapp even redirected my in my browser to the URL with the line breaks characters completely removed from the parameter.Eskimoaleut
Thanks anyway for your help. For now I'll stop working on it. Maybe in coming days I'll be more free to spend more time on it.Moises
M
4

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>
Mctyre answered 21/2, 2020 at 5:57 Comment(0)
M
3

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 }}))
Moises answered 15/2, 2018 at 10:23 Comment(0)
T
0

You can use urlencode($yourmessage) for same.

Training answered 15/2, 2018 at 7:40 Comment(2)
The problem is line-break which still is not fixed.Moises
I tried it too: The same. Here is the output: Moonlight-263-Bra%5Cnhttp%3A%2F%2Fwww.mysite.com%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D55Moises
I
0

i tried with "\\n" (double backslash + n), and it's worked !

i get this from send test message at insomnia (postman like) with php-curl API to whatsapp number.

Illusage answered 16/6, 2023 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.