如果詢問了此問題,我很抱歉,但我找不到可行的解決方案。我正在開發一個留言板系統,它已經完成並且正在工作,但是我需要將它貶低,我現在已經設置好了去除會破壞sql等等的所有內容,但允許強,em,img和a。我的問題是,如果我使用標籤,它工作正常,但人們有時不知道如何使用它,所以,而不是我不得不顯示文本解釋它,我只是希望PHP爲我解決它。因此,用戶可以剪切並粘貼地址,然後輸入文本,並在需要時輸入更多地址,然後提交表單。當它顯示時,我想要超鏈接工作。現在,如果我使用標籤,它可以正常工作,但如果我不使用標籤,它只是吐出普通文本。MySQL值包含鏈接,無法將文本更改爲超鏈接(我的標題很糟糕)
我看遍了所有的地方,沒有任何作品。我發生的事情最多的是該帖子根本沒有出現。如果有不同的語言可以更容易地做到這一點,我願意與它合作,但現在我專注於PHP和MySQL來完成這項工作。如果需要的話,我願意發佈代碼,但是我不想用大量的數據來淹沒這個帖子,這些數據不會真的有幫助。我只是需要它來搜索身體數據找到鏈接變成一個鏈接,然後吐出它周圍的一切。
要清除某些東西,我正在使用包含文件夾中的方法。每個普通頁面只有一小部分代碼與我需要的代碼有關。所以forum.php沒有真正的設計,但是拉出了一個頭文件和頁腳文件,它包含了所有設計和靜態內容,這些內容在頁面間不會改變。現在,read.php文件將提取includes文件夾中的所有文件,並在需要時應用它們。所以我使用的代碼:
public static function url_to_link($text) {
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if (preg_match_all($reg_exUrl, $text, $url)) {
// make the urls hyper links
foreach($url[0] as $v){
//current position of the searached url
$curpos = strpos($text,' '.$v)+1;
//delete the url
$text = substr_replace($text,'', $curpos, strlen($v));
//insert the link
$text = substr_replace($text,''.$v.'', $curpos ,0);
}
return $text;
}
else {
// if no urls in the text just return the text
return $text;
}
}
無論如何他們中的一個是在includes/comment.php文件中。還有其他的函數需要thread_id並將其傳遞給read.php頁面並顯示對該線程的回覆。現在,在read.php文件我有這樣的代碼:
<div id="comments">
<?php foreach($comments as $comment): ?>
<div class="comment" style="margin-bottom: 2em;">
<div class="author">
<a href='profile.php?id=<?php echo $diuser[0] ?> '><?php echo htmlentities($comment->author) ?></a> wrote:
</div>
<div class="body">
<?php echo strip_tags($comment->body, '<strong><em><p><a><img>'); ?>
</div>
<div class="meta-info" style="font-size: 0.8em;">
<?php echo datetime_to_text($comment->posted); ?>
</div>
____________________________
</div>
<?php endforeach;
if(empty($comments)) { echo "No comments."; } ?>
</div>
然後我改變了調用新的功能,我添加:
<div id="comments">
<?php foreach($comments as $comment): ?>
<div class="comment" style="margin-bottom: 2em;">
<div class="author">
<a href='profile.php?id=<?php echo $diuser[0] ?> '><?php echo htmlentities($comment->author) ?></a> wrote:
</div>
<div class="body">
<?php $comment_body = Comment::url_to_link($comment->body);
echo nl2br($comment_body); ?>
</div>
<div class="meta-info" style="font-size: 0.8em;">
<?php echo datetime_to_text($comment->posted); ?>
</div>
____________________________
</div>
這會產生一些看起來像這樣:
user wrote:
Thttp://www.example.com
Testing links.
June 28, 2013 at 03:09 PM
____________________________
數據應顯示爲
user wrote:
Testing
http://www.example.com
Testing Links.
Date (you get the idea)
因此,代碼正在做一些事情,但它不會將數據變成鏈接,它只是刪除一些數據,然後將它們放在一起。我知道我做錯了什麼,但我弄不明白。
是的我使用過htmlentities和mysql_real_escape_string。請幫幫我。
- 編輯 -
TL; DR =用戶輸入下面進入論壇:
testing http://stackoverflow.com testing links
我希望鏈接輸出工作,但我只想要實際的鏈接顯示出來作爲一個鏈接;它應該看起來像這樣
testing <a href="http://stackoverflow.com">http://stackoverflow.com</a> testing links
但是我找到的代碼不起作用。我是否需要爲鏈接添加一個輸入來實現這個功能,我可以這麼做,但我寧願用戶只需在正文輸入中輸入url,然後代碼將其轉換爲輸出中的實際鏈接。我目前擁有的代碼已在上面,但它仍然無效。
你在找這樣的事嗎? http://regexr.com?35d3g –
不,我有。 – DataCorrupt