te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to make a `text + line-break + link` for sharing on Whatsapp? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a `text + line-break + link` for sharing on Whatsapp? - Stack Overflow

programmeradmin4浏览0评论

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:

.myweb%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D61

According to this page () it's possible to use window.encodeURIComponent(whatsappMessage) to have a line-break, but I don't know how to bine it with my php code or use it in html side:

<a href="={{ 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 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./send?text=Nurinu%20-%201310%20-%20Bra%5Cr%5Cnhttp%3A%2F%2Fwww.myweb.%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D61

According to this page (https://github./kriskbx/whatsapp-sharing/issues/16#issuement-294854157) it's possible to use window.encodeURIComponent(whatsappMessage) to have a line-break, but I don't know how to bine it with my php code or use it in html side:

<a href="https://api.whatsapp./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);
}
Share Improve this question edited Feb 15, 2018 at 7:48 Kardo asked Feb 15, 2018 at 7:24 KardoKardo 1,7185 gold badges33 silver badges56 bronze badges 8
  • '\r\n' - that’s not a line break. – C3roe Commented Feb 15, 2018 at 7:39
  • @CBroe: So what should I use instead? – Kardo Commented Feb 15, 2018 at 7:50
  • Your knowledge of the PHP syntax basics …? php/manual/en/language.types.string.php – C3roe Commented Feb 15, 2018 at 7:56
  • Here is the output using rawurlencode: Here is the output: Moonlight-263-Bra%5Cnhttp%3A%2F%2Fwww.mysite.%2Findex.php%3Froute%3Dproduct%2Fproduct%26amp%3Bproduct_id%3D55 Any idea how to make a line break? – Kardo Commented Feb 15, 2018 at 8:03
  • Read the link to the manual that I posted ... and pay attention to the difference between strings enclosed in single, or in double quotes ... – C3roe Commented Feb 15, 2018 at 8:13
 |  Show 3 more ments

4 Answers 4

Reset to default 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 mand: %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 mands 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.';

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

I fixed the issue according to this article (http://webdevelopmentscripts./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./index.php?route=product/product&product_id=46

Also I could use encodeURIComponent:

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

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.

You can use urlencode($yourmessage) for same.

发布评论

评论列表(0)

  1. 暂无评论