Hello, I am trying to find a way to make any URL's entered into the comments box on my site to automatically become linked and clickable after they are posted, without requiring the person entering the URL to enter any tags or HTML.
I came across a few tutorials, sort of, online and am having difficulty getting them to work for me. The comments are stored using a database. Here is my page code for the comments:
<?php
require($_SERVER['DOCUMENT_ROOT'].'/'.'init.php');
RequireLogin();
if($IsPostBack)
{
if($_POST['post'] == 'Cancel')
{
$returnUrl = urldecode(Context::Current()->NextPage) . "#" . $drawing->Id;
header("Location: " . urldecode($returnUrl));
die;
}
$comment = $_POST['comment'];
$drawingId = $_POST['DrawingId'];
$drawing = Drawing::Load($drawingId);
if(!empty($drawing))
{
if(!empty($comment))
{
$drawing->AddComment(Context::Current()->CurrentUser->Id, $comment);
$returnUrl = urldecode(Context::Current()->NextPage) . "#" . $drawing->Id;
header("Location: " . urldecode($returnUrl));
die;
}
else
{
$ErrorMessage = "Comment cannot be empty.";
}
}
}
else
{
$drawingId=0;
if(isset($_GET['did']))
if(ctype_digit($_GET['did']))
$drawingId = $_GET['did'];
if($drawingId == 0)
{
$returnUrl = $appRoot.'/index.php';
header("Location: " . urldecode($returnUrl));
die;
}
else if($drawingId > 0)
{
$drawing = Drawing::Load($drawingId);
//Context::Current()->CurrentDrawing = $drawing;
}
else if(empty($drawing)){
$returnUrl = $appRoot.'/index.php';
header("Location: " . urldecode($returnUrl));
die;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link type="text/css" rel="stylesheet" href="../styles/standard.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Add Comment</title>
</head>
<body>
<?php showStandardHeader(false); ?>
<div class="contentWrapper" >
<div class="content">
<div class="pageTitle">Add Comment</div>
<?php if($ErrorMessage) { ?>
<div style="color:red"><?php echo $ErrorMessage; ?></div>
<?php } ?>
<div class="boardItem">
<div class="drawingTitleBar">
<div class="drawingNumber"><?php echo "[" . $drawing->DrawingNumber . "]"; ?></div>
<div class="drawingTitle">
<?php echo $drawing->Title ? $drawing->Title : "Untitled"; ?>
<?php echo " - by " ?>
<?php CreateUserNameLinks($drawing->AllUserNames()); ?>
<?php $time = $drawing->TotalTimeFormatted; if(!empty($time)) echo " - " . str_replace(" ", " ", $drawing->TotalTimeFormatted); ?>
</div>
<div style="clear:both;"></div>
</div>
<div style="clear:both;border: #d1d1d1 dotted 1px;min-height:321px;min-heigh\t:324px;">
<div class="boardImageCol" >
<div style="padding:10px;margin:0;">
<a href="<?php echo $appRoot . '/Drawing/viewDrawing.php?did=' . $drawing->Id; ?>">
<img border="0" src="<?php echo $images_prefix . '/' . $drawing->Current->FilePath;?>" />
</a>
</div>
<div class="boardIcons" style="clear: both;padding: 5px 5px 5px 10px;margin: 0;border-top-style:dotted;border-top-width:1px;border-top-color:#d1d1d1;">
</div><div style="clear:both;">
</div>
</div>
<div class="boardCommentCol" >
<form method=post action='index.php' id="login">
<div style="padding:0 0 10px 0;"><textarea rows="10" cols="50" name="comment"></textarea></div>
<div style="padding:0 0 10px 0;"><input name="post" type="submit" value="Post Comment" > <input name="post" type="submit" value="Cancel" ></div>
<input name="IsPostBack" type="hidden" value="true">
<input name="DrawingId" type="hidden" value="<?php echo $drawingId; ?>">
</form>
<div id="boardComments">
<?php include($_SERVER['DOCUMENT_ROOT'] . $appRoot . '/' . 'Drawing/comments.php'); ?>
</div>
</div>
<div style="clear:both;"></div>
</div>
</div>
</div>
<div style="clear:both"></div>
</div>
</body>
</html>
One tutorial suggested this code, I'm not certain how to implement it:
function makeClickableLinks($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])([url]www.[-a-zA-Z0-9@:%_\+.~#?&//=]+[/url])',
'\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'<a href="mailto:\\1" target=_blank>\\1</a>', $text);
return $text;
}
// Example Usage
// echo makeClickableLinks("This is a test clickable link: http://www.websewak.com You can also try using an email address like test@websewak.com");
Others have suggessted using preg_replace. I am not clear on how to implement this, into my code.