0

My title text has this character | and hence twitter share isn't working.

https://twitter.com/intent/tweet?text=Wall%20Clock%20|%20by%20BestPlace%20http://www.example.com/wall-clock&hashtags=BestPlace

This is the PHP code:

<a href="https://twitter.com/intent/tweet?text=<?php echo $heading_title; ?>%20<?php echo 'http://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];?>&amp;hashtags=fufuh" target="_blank" class="btn btn-default">
           Share Twitter
        </a>

How do I replace or remove | from the text so twitter would load it properly?

Elaine Byene
  • 3,868
  • 12
  • 50
  • 96
  • 1
    You may need to force the encode on the pipe character via `` or do `` – Scuzzy Sep 21 '17 at 01:20
  • If you want to embed something in a URL you should [`urlencode()`](http://php.net/manual/en/function.urlencode.php) it. – Sammitch Sep 21 '17 at 01:21
  • 1
    Funny though that there is already `%20` between "Wall Clock"... `` to prevent double encoding maybe? – Scuzzy Sep 21 '17 at 01:21

1 Answers1

1

I would want to know why your $heading_title already contains partial url encoded value...

Remove/Replace option:

<?php echo str_replace('|','',$heading_title); ?>

Re-Encode option:

This will remove the current encoding (because your title has %20 for space) then re-encode it to catch the pipe character.

<?php echo urlencode(urldecode($heading_title)); ?>

or possibly rawurlencode to retain the percent notation ( %20 vs + )

<?php echo rawurlencode(urldecode($heading_title)); ?>
Scuzzy
  • 12,186
  • 1
  • 46
  • 46