Create line break in WhatsApp message

Create line break in WhatsApp message

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why nl2br Breaks WhatsApp Web Share Links

When building a web application that integrates with WhatsApp, you often encounter a specific friction point: formatting text within a shareable link. You might be using a library like whatsapp-button.js to generate a button that pre-fills the message body. The goal is simple—display product details on multiple lines so users don't have to scroll horizontally or squint at a massive paragraph.

However, there is a fundamental misunderstanding regarding how WhatsApp's wa.me or whatsapp://send protocol handles input data. The confusion usually stems from mixing standard HTML rendering rules with raw URL encoding rules.

The Core Issue: Raw URLs vs. Rendered HTML

The code snippet you provided attempts to solve this by echoing the PHP string variable directly into the data-text attribute, wrapped in an nl2br() function call:

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

// The problematic line
<a href="whatsapp://send" data-text="<?php echo nl2br($_u); ?>" ...>Share</a>

The logic here assumes that because nl2br() converts newline characters (\n) into HTML <br> tags, WhatsApp will render those tags. This works perfectly fine if you are opening a new browser tab or viewing content inside an iframe where the browser is responsible for rendering the HTML string found in the data-text attribute.

However, when this link is clicked on mobile devices (which is 90% of your traffic), WhatsApp does not render the HTML contained within the URL parameters as a web page. Instead, it treats the entire string passed to data-text as raw text for the message body. Since <br> is an HTML tag and not a literal character sequence, the user sees the exact characters <, b, r, and >.

Furthermore, even if you strip the HTML tags entirely, simply echoing \n or %0D%0A into the URL often fails because WhatsApp's input parsing strips whitespace and control characters before sending the message to their servers. They aggressively normalize text inputs to prevent line breaks in shared messages unless explicitly told otherwise via a specific format.

The Solution: HTML Entities and Pre-Tagging

To successfully force line breaks when using the whatsapp://send URI scheme, you must bypass the raw text stripping and inject valid HTML directly into the message body. WhatsApp allows HTML tags within the message content if they are properly encoded within the URL parameters.

The strategy is to convert your PHP string into a format that survives URL encoding and then explicitly wrap it in <p> or <br> tags before sending.

You should stop using nl2br() on the raw variable output. Instead, construct the HTML string directly in PHP, ensuring newlines are represented by the HTML break tag <br>. Crucially, you must URL-encode the special characters in your final string so that the browser doesn't interpret them as part of the URL path or query structure incorrectly.

Here is how you should refactor the generation logic:

$_u = "Product Name: ".$_productName;
$_u .= "<br>Sku: ".$_productSku;

// Create a safe version for the data-text attribute
$encodedMessage = rawurlencode($_u);

// Generate the link using whatsapp.com/web/send?text instead of the app:// scheme 
// which is more reliable for web-to-app handoff on modern browsers.
$link = "https://wa.me/?text={$encodedMessage}";

// Or if strictly sticking to your library's expected href format:
$a_href = "whatsapp://send?text=" . rawurlencode($_u);

echo "<a href=\"{$link}\" class='wa_btn'>Share</a>";

By using rawurlencode(), you ensure that the <br> characters are not misinterpreted. You also explicitly define the HTML tags (<br>) rather than relying on newline escape sequences like \n. When WhatsApp parses this URL, it accepts the HTML tags and renders the line breaks as intended.

Trade-offs and Compatibility Notes

There is a significant trade-off between using the whatsapp:// scheme versus the https://wa.me/ scheme. The whatsapp:// scheme attempts to launch the native desktop or mobile app immediately. If the user does not have the app installed, the link fails silently or prompts an app store download in some browsers, but often nothing happens on iOS.

The https://wa.me/ approach opens WhatsApp Web in the browser first if available, or redirects to the app. This method is generally more robust for handling the HTML payload because the browser's address bar handles the URL encoding more predictably than older mobile webviews.

Additionally, be aware that WhatsApp occasionally updates their parsing logic. If you find that <br> tags are being stripped in a future update, the fallback mechanism usually involves ensuring the text is short enough to fit within a single line or using emojis to separate lines if HTML injection becomes restricted. However, currently, injecting URL-encoded HTML break tags remains the standard solution for multi-line messages via link sharing.

Avoid relying on PHP_EOL or \r\n inside the data-text attribute without explicit HTML conversion, as these are invisible to the WhatsApp parser in this context. The only way to guarantee a visual line break is to send the literal string <br>.

Our network