Ravi Patel
Ravi Patel

Reputation: 5211

Create line break in WhatsApp message

I have been working on WhatsApp sharing message using:

whatsapp-button.js

$_u.="Product Name:".$_productName."\n";

$_u.="Sku:".$_productSku."\n";

<a href="whatsapp://send" data-text="<?php echo nl2br($_u); ?>" data-href="" class="wa_btn <?php echo $buttonsize; ?>" style="display:none">Share</a>

How to add a line break:

I have tried \n, \r\n, PHP_EOL, and %0D%0A, but it just displays as text.

Upvotes: 35

Views: 126993

Answers (6)

ARGVN
ARGVN

Reputation: 41

Try to use &#13;. It is a special HTML code.

For example:

Hello!&#13;How are you today?

will get a result like this:

Hello!
How are you today?

Upvotes: 0

Pablo Mariante
Pablo Mariante

Reputation: 400

https://wa.me/+99123456789?text=Line%0aBreak

The %0a represents the line break

Upvotes: 22

Jayesh Sonpal
Jayesh Sonpal

Reputation: 411

To create a line break in WhatsApp you can use this command. It's working fine and I am using it:

use `%0a`

For example:

smsContain = "*Greetings from  " + cname + " ,%0a %0aM/s. " + txtName.Text + " %0a %0aYour Bill for Advertisement is generated ; %0a %0aBill Date  :-  " + DateTime.ParseExact(dateTimePicker1.Text, "dd/MM/yyyy", null).ToString("dd/MM/yyyy") + " %0a %0aBill no  :-  " + lblBillNo.Text + "  %0a %0aBilling Amount of Rs.  " + lblNet_Amt.Text + " %0a %0aAdvertisement Published in  " + news + " in " + Edi + "  edition,%0a %0aReleased Date : " + DateTime.ParseExact(DateTime.Parse(dt).ToShortDateString(), "dd/MM/yyyy", null).ToString("dd/MM/yyyy") + ".%0a %0aPlease find the Bill attached below, and request you to please  release the payment ASAP. %0a %0a %0aAny descripancy in regards to this Bill to reported to us immediately.%0a %0a %0aAlways at your Service....* ";
                smsContain = smsContain.Replace("&", "+%26+");

Upvotes: 31

am2505
am2505

Reputation: 2394

If you want to send an only text containing newline

use this %0a


link =`whatsapp://send?text=%0a‎Hello%0aWorld`;

If you want to send some url link with text containing newline

var encodedURL = encodeURIComponent(some_url);
link =`whatsapp://send?text=${encodedURL}%0a‎Hello%0aWorld`;

Now embedded this link in anchor tag

<a href=link> Click here! </a>

Upvotes: 87

Rathma
Rathma

Reputation: 1313

There is a solution in here which basically is using:

whatsappMessage = window.encodeURIComponent(whatsappMessage)

Upvotes: 2

Vishnu Sharma
Vishnu Sharma

Reputation: 642

I got one working solution:

HTML:

$_u.="Product Name:".$_productName."\n";
$_u.="Sku:".$_productSku."\n";

<a data-text="<?php echo $_u; ?>" data-link="" class="whatsapp">Share</a>

JS:

           var isMobile = {
                Android: function() {
                    return navigator.userAgent.match(/Android/i);
                },
                BlackBerry: function() {
                    return navigator.userAgent.match(/BlackBerry/i);
                },
                iOS: function() {
                    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                },
                Opera: function() {
                    return navigator.userAgent.match(/Opera Mini/i);
                },
                Windows: function() {
                    return navigator.userAgent.match(/IEMobile/i);
                },
                any: function() {
                    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                }
                };
                $(document).on("click", '.whatsapp', function() {
                    if( isMobile.any() ) {

                        var text = $(this).attr("data-text");
                        var url = $(this).attr("data-link");
                        var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
                        var whatsapp_url = "whatsapp://send?text=" + message;
                        window.location.href = whatsapp_url;
                    } else {
                        alert("Please share this in mobile device");
                    }

                });

Upvotes: 3

Related Questions