如果询问了此问题,我很抱歉,但我找不到可行的解决方案。我正在开发一个留言板系统,它已经完成并且正在工作,但是我需要将它贬低,我现在已经设置好了去除会破坏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