【问题标题】:How to create Twitter Share link for my website containing short blogs如何为我的包含短博客的网站创建 Twitter 分享链接
【发布时间】:2021-06-29 02:14:52
【问题描述】:

我想从我的网站创建一个 Twitter 共享链接,通过从 PHP 数据库中获取数据来共享用户博客的前几行。博客的数据以 HTML 格式存储在数据库中。我尝试获取数据并将其转换为 HTML 解码字符,但是当我尝试使用转换后的字符到 Twitter 链接时,它会在发布推文之前重新添加 HTML 字符/标签。附上图片供参考。

<script>
 
    function share(a) {
              let share_poem_ID = "#share-modal-" + a;
              document.querySelector(share_poem_ID).style.display = "block";
            }
    
    function shareClose(b) {
              let share_poem_ID = "#share-modal-" + b;
              document.querySelector(share_poem_ID).style.display = "none";
            }
</script>

<?php
    
    include 'connection.php';
    session_start();
    
    $query = " Select * From $dbtable";
    $result=@mysqli_query($connection,$query);
    $row=mysqli_fetch_assoc($result);
    
    foreach ($result as $row) {
     $Poem = $row['Poem'];
     $PoemShare =  htmlspecialchars_decode($Poem);
       echo "
        <div>
         <div class='the-poem'>
          <div class='poem'>$Poem</div>
         </div>
    
         <div class='poem-options'>
          <a class='share poem-option-list' id='report-btn-$ID' data-field-id='$ID' onclick='share($ID)'><i class='mdi mdi-share'></i></a>
         </div>
        </div>
    
      <!--- Share Modal --->
        <div class='modal-share' id='share-modal-$ID' role='dialog'>
         <div id='modal_content-share' class='modal-content-share'>
          <div class='page-container' id='page-content'>
            <span id='twitter$ID' class='share-option'>
              <i class='mdi mdi-twitter'></i>
                Share via Twitter
             </span>
           <script>
               $('#twitter$ID').on('click',function(){
                  window.open('https://twitter.com/intent/tweet?url=https://www.MartianJK.com/ms/login.php?u=$ID&text='$PoemShare'&hashtags=SpreadingSmiles', '_blank').focus();
                })
          </script>
         </div>      
        </div>
       </div>            
      ";
    
    }
    ?>

【问题讨论】:

    标签: php html hyperlink share


    【解决方案1】:

    你可以使用strip_tags():

    $PoemShare =  strip_tags($Poem);
    

    或者保留&lt;br&gt;标签:

    $PoemShare =  strip_tags($Poem, '<br>');
    

    【讨论】:

    • 你的答案是正确的,但它也会去掉
      标签并在一行中显示所有内容。我希望
      标签/新行按原样运行...
    • @JainishKothary 我已经更新了答案以保留&lt;br&gt; 标签。
    • 感谢您的帮助,但我们能否以某种方式将
      转换为 HTML 链接的 %0A?
    • 您也许可以使用$Poem = str_replace('&lt;br&gt;', "\n", $Poem); 也可以尝试使用urlencode()"\n" 转换为%0A
    • 我刚刚添加了这些行,效果很好。感谢您的帮助:$PoemShare = strlen($Poem) &gt; 99 ? substr($Poem,0,100).'...' : $Poem; $PoemShare = strip_tags($PoemShare, '&lt;br&gt;'); $PoemShare = preg_replace('&lt;&lt;br /&gt;&gt;','%0A', $PoemShare); $PoemShare = str_replace('&amp;','%26', $PoemShare); $PoemShare = str_replace('"','%22', $PoemShare); $PoemShare = str_replace("'",'%27', $PoemShare); echo var_dump($PoemShare);
    【解决方案2】:
    $PoemShare = strlen($Poem) > 99 ? substr($Poem,0,100).'...' : $Poem;    
    $PoemShare = strip_tags($PoemShare, '<br>');
    $PoemShare = preg_replace('<<br />>','%0A', $PoemShare);
    $PoemShare = str_replace('&','%26', $PoemShare);
    $PoemShare = str_replace('"','%22', $PoemShare);
    $PoemShare = str_replace("'",'%27', $PoemShare);
    echo var_dump($PoemShare); 
    

    这段代码完成了我的工作。希望它也对其他人有所帮助。归功于用户@Syscall

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      相关资源
      最近更新 更多