Php 如何为我的包含短博客的网站创建Twitter共享链接
我想从我的网站上创建一个Twitter共享链接,通过从PHP数据库获取数据,让用户共享博客的前几行。博客的数据以HTML格式存储在数据库中。我尝试获取数据并将其转换为HTML解码字符,但当我尝试使用转换后的字符到Twitter链接时,它会在发布tweet之前重新添加HTML字符/标记。附图供参考Php 如何为我的包含短博客的网站创建Twitter共享链接,php,html,hyperlink,share,Php,Html,Hyperlink,Share,我想从我的网站上创建一个Twitter共享链接,通过从PHP数据库获取数据,让用户共享博客的前几行。博客的数据以HTML格式存储在数据库中。我尝试获取数据并将其转换为HTML解码字符,但当我尝试使用转换后的字符到Twitter链接时,它会在发布tweet之前重新添加HTML字符/标记。附图供参考 <script> function share(a) { let share_poem_ID = "#share-modal-"
<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>
";
}
?>
功能共享(a){
让我们分享你的诗意吧!“#分享模态-”+a;
document.querySelector(share\u\u ID).style.display=“block”;
}
功能共享关闭(b){
让我们分享你的诗篇(ID=“#分享模态-”+b;
document.querySelector(share\u\u ID).style.display=“无”;
}
您可以使用:
或保留
标签:
$PoemShare = strip_tags($Poem, '<br>');
$PoemShare=strip_标签($Poem,
);
$PoemShare=strlen($Poem)>99?substr($Poem,0100)。“…”:$Poem;
$PoemShare=strip_标签($PoemShare,
);
$PoemShare=preg_replace(“
”、“%0A”、$PoemShare);
$PoemShare=str_replace('&','%26',$PoemShare);
$PoemShare=str_replace(“”、“%22”、$PoemShare);
$PoemShare=str_replace(“'”、“%27'、$PoemShare);
echo var_dump($PoemShare);
这段代码让我完成了工作。希望它也能帮助其他人。感谢用户@Syscall您的答案是正确的,但它也去掉了
标记,并在一行中显示了所有内容。我希望
标记/新行按原样运行…@JainishKothary我已经更新了答案,以保留
标记。谢谢您的帮助,但我们可以将
转换为%0A吗对于HTML链接,您可以使用$Poem=str_replace(“
”,“\n”,$Poem);
也可以尝试urlencode()
将“\n”
转换为%0A
。我刚刚添加了这些行,效果非常好。感谢您的帮助:$PoemShare=strlen($Poem)>99?substr($Poem,0100)“…”:$Poem;$PoemShare=strip_tags($PoemShare,
);$PoemShare=preg_replace(“
,'%0A',$PoemShare);$PoemShare=str_replace('&','%26',$PoemShare);$PoemShare=str replace(“,'%22',$PoemShare)$PoemShare=str_replace(“'”、“%27'、$PoemShare);echo var_dump($PoemShare)代码>
$PoemShare = strip_tags($Poem, '<br>');
$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);