【问题标题】:How to make a `text + line-break + link` for sharing on Whatsapp?如何制作“文本+换行+链接”以在 Whatsapp 上共享?
【发布时间】:2018-07-25 21:27:37
【问题描述】:

我在网上尝试了很多方法来解释这个问题,但没有找到适合我需要的方法。

我想在我的网站上为每个产品创建一个share to whatsapp 链接,包括product name, line-break and link。像这样的:

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

我正在使用 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']))

以上代码返回:

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

根据此页面 (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>

更新

我忘了包含函数(encodeURIComponent):

function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}

【问题讨论】:

  • '\r\n' - 这不是换行符。
  • @CBroe:那我应该改用什么?
  • 您了解 PHP 语法基础知识……? php.net/manual/en/language.types.string.php
  • 这是使用rawurlencode的输出: 这是输出:Moonlight-263-Bra%5Cnhttp%3A%2F%2Fwww.mysite.com%2Findex.php%3Froute%3Dproduct%2Fproduct% 26amp%3Bproduct_id%3D55 知道如何换行吗?
  • 阅读我发布的手册链接...并注意用单引号或双引号括起来的字符串之间的区别...

标签: javascript php hyperlink whatsapp encodeuricomponent


【解决方案1】:

我根据这篇文章(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>

结果正是我想要的:

Moonslictese-251-Bra
http://www.example.com/index.php?route=product/product&product_id=46

我也可以使用encodeURIComponent:

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

【讨论】:

    【解决方案2】:

    2020 年更新

    尽管这种趋势已经有好几年了,但我还是在这里寻找同样的问题。所以这是今天的当前方法:

    Spaces 使用此命令:%20(但如果在 PHP 变量中则不需要)

    换行符:%0A 或 %0D%0A(完全需要)

    链接:不需要特殊字符

    $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>
    

    【讨论】:

      【解决方案3】:

      您也可以使用urlencode($yourmessage)。

      【讨论】:

      • 问题是line-break 仍然没有解决。
      • 我也试过了:一样。这是输出:Moonlight-263-Bra%5Cnhttp%3A%2F%2Fwww.mysite.com%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D55
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多