如果有人问我这个问题,我很抱歉,但我真的找不到有效的解决方案。我正在开发一个留言板系统,它已经完成并工作了,但我需要让它安静下来,我目前已经设置好了它,可以剥离所有会破坏sql等但允许strong、em、img和a的内容。我的问题是,如果我使用标记,它会很好地工作,但人们有时不知道如何使用它,所以我不必显示解释它的文本,我只想让php为我修复它。这样用户就可以剪切和粘贴地址,然后输入文本,如果需要的话,可以有更多的地址,然后提交表单。当它显示出来时,我希望超链接能正常工作。现在,如果我使用标签,它可以正常工作,但如果我不使用标签,就会吐出正常的文本。
我到处看了一遍,什么都不管用。我所经历的最糟糕的事情就是帖子根本没有出现。如果有不同的语言可以更容易地做到这一点,我愿意使用它,但现在我正专注于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_eescape_string。请帮帮我。
--编辑--
tl;dr=用户在论坛中输入以下内容:
testing http://stackoverflow.com testing links
我希望链接在输出中工作,但我只希望实际链接显示为链接;它应该像这个
testing <a href="http://stackoverflow.com">http://stackoverflow.com</a> testing links
但是我找到的代码不起作用。我必须为链接添加一个输入吗?我可以这样做,但我更希望用户只在主体输入中键入url,然后代码会在输出中将其转换为实际的链接。我目前拥有的代码在上面,同样它不起作用。
您不需要查看MySQL。数据库很好。
您需要查看的是正则表达式和其他文本解析功能,以搜索注释中的链接,然后输出被标记包围的注释:
这相当简单。然而,这并不是微不足道的,它的实际结果是,大多数网站只是使用BBCode或发明了一种简单的语法(StackOverflow和Reddit都使用括号环绕链接的组合(尽管StackOverload也检测链接))。
preg_replace
可能会满足您的需求。
请重写你的长篇大论,将其浓缩为包含:
- 您正在处理的实际文本
- 预期输出(带有HTML字符)
删除对数据库的任何引用。您已经从数据库中获取了数据,信息的来源现在已经无关紧要了。
我只是想更新它,这样其他人就会知道如何让它发挥作用,因为我以前没有得到任何真正的帮助。
首先,我使用PHP OO方法。
示例:
Comment.php // has the makeLinks code
public static function makeLinks($str) {
return preg_replace('/(https?):'/'/([A-Za-z0-9'._'-'/'?=&;%,]+)/i', '<a href="$1://$2" target="_blank">$1://$2</a>', $str);
}
该函数将在其他需要makeLinks命令的文件中调用,看起来如下:
$comment_body = Comment::makeLinks(strip_tags($comment->body, '<strong><em><p><a><img>'));
echo nl2br($comment_body);
我希望这是有道理的,希望有人能发现它有用。