向文本区域字段添加自动换行符和超链接


Adding automatic line breaks and hyperlinks to textarea field?

我有一个Joomla网站,它使用一些简单的php来提交表单,但我希望能够在我输入文本时按回车键时自动添加换行符,以及自动将任何网址转换为可点击的链接。这可能吗,谁能告诉我如何请?

    <?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_spectac = "localhost";
$database_spectac = "database";
$username_spectac = "username";
$password_spectac = "password";
$spectac = mysql_pconnect($hostname_spectac, $username_spectac, $password_spectac) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
<?php
if(isset($_POST['eventDate'])){
$date = date('Y-m-d',strtotime($_POST['eventDate']));
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE tblEventCustom SET eventDate=%s, eventFile=%s, eventActive=%s, eventCost=%s, eventName=%s, eventFriendlyName=%s, eventDesc=%s WHERE eventId=%s",
                       GetSQLValueString($date, "date"),
                       GetSQLValueString($_POST['eventFile'], "text"),
                       GetSQLValueString($_POST['eventActive'], "int"),
                       GetSQLValueString($_POST['eventCost'], "int"),
                       GetSQLValueString($_POST['eventName'], "text"),
                       GetSQLValueString($_POST['eventFriendlyName'], "text"),
                       GetSQLValueString($_POST['eventDesc'], "text"),
                       GetSQLValueString($_POST['eventId'], "int"));
  mysql_select_db($database_spectac, $spectac);
  $Result1 = mysql_query($updateSQL, $spectac) or die(mysql_error());
}
$colname_getEventDetails = "-1";
if (isset($_POST['eventId'])) {
  $colname_getEventDetails = $_POST['eventId'];
}
mysql_select_db($database_spectac, $spectac);
$query_getEventDetails = sprintf("SELECT * FROM tblEventCustom WHERE eventId = %s", GetSQLValueString($colname_getEventDetails, "int"));
$getEventDetails = mysql_query($query_getEventDetails, $spectac) or die(mysql_error());
$row_getEventDetails = mysql_fetch_assoc($getEventDetails);
$totalRows_getEventDetails = mysql_num_rows($getEventDetails);
mysql_select_db($database_spectac, $spectac);
$query_getFileList = "SELECT * FROM tbl_eventFiles";
$getFileList = mysql_query($query_getFileList, $spectac) or die(mysql_error());
$row_getFileList = mysql_fetch_assoc($getFileList);
$totalRows_getFileList = mysql_num_rows($getFileList);

?>
 <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
</script>
<link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
    $(function() {
        $( "#datepicker" ).datepicker();
        $(eventDate).datepicker({dateFormat: 'yy-mm-dd'})
    });
    </script>
    <? if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { ?><div align="center" >Event Updated <br>
      <a href="http://spectaculareventcenter.com/2012/index.php/component/content/article/2-uncategorised/22-editevents">Go back to event list. </a></div><? } ?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">ID:</td>
      <td><?php echo $row_getEventDetails['eventId']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event Date:</td>
      <td><input type="text" name="eventDate" value="<?php echo htmlentities($row_getEventDetails['eventDate'], ENT_COMPAT, ''); ?>" size="32" id="datepicker"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event Active:</td>
      <td><p>
        <label>
          <input <?php if (!(strcmp($row_getEventDetails['eventActive'],"1"))) {echo "checked='"checked'"";} ?> type="radio" name="eventActive" value="1" id="RadioGroup1_0">
          Yes</label>
        <br>
        <label>
          <input <?php if (!(strcmp($row_getEventDetails['eventActive'],"0"))) {echo "checked='"checked'"";} ?> type="radio" name="eventActive" value="0" id="RadioGroup1_1">
          No</label>
        <br>
      </p></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event Cost:</td>
      <td><input type="text" name="eventCost" value="<?php echo htmlentities($row_getEventDetails['eventCost'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event Name:</td>
      <td><input type="text" name="eventName" value="<?php echo htmlentities($row_getEventDetails['eventName'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Display Name:</td>
      <td><input type="text" name="eventFriendlyName" value="<?php echo htmlentities($row_getEventDetails['eventFriendlyName'], ENT_COMPAT, ''); ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event Desc:</td>
      <td><textarea cols=32 rows=5 name="eventDesc" value="<?php echo htmlentities($row_getEventDetails['eventDesc'], ENT_COMPAT, ''); ?>" size="32"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Event File:</td>
      <td><select name="eventFile">
        <?php
do {  
?>
        <option value="<?php echo $row_getFileList['fileName']?>"<?php if (!(strcmp($row_getFileList['fileName'], $row_getEventDetails['eventFile']))) {echo "selected='"selected'"";} ?>><?php echo $row_getFileList['fileName']?></option>
        <?php
} while ($row_getFileList = mysql_fetch_assoc($getFileList));
  $rows = mysql_num_rows($getFileList);
  if($rows > 0) {
      mysql_data_seek($getFileList, 0);
      $row_getFileList = mysql_fetch_assoc($getFileList);
  }
?>
      </select>
      <a href="http://spectaculareventcenter.com/2012/customCode/file_upload.php" target="_blank">Upload File</a></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Update record"></td>
    </tr>
  </table>
  <p>
    <input type="hidden" name="MM_update" value="form1">
    <input type="hidden" name="eventId" value="<?php echo $row_getEventDetails['eventId']; ?>">
  </p>
</form>
<?php
mysql_free_result($getEventDetails);
mysql_free_result($getFileList);
?>

在将输入字符串存储到数据库中之前,对输入字符串使用 nl2br。至于自动链接,用空格分解输入字符串,遍历所有单词(或使用 array_walk())并调用

filter_var($yourstring, FILTER_VALIDATE_URL);

在上面。如果它返回 true,则只需在其周围添加 html 链接标签并替换数组元素。最后,内爆数组以获取用于存储在数据库中的明文。

这是我通过快速谷歌搜索找到的添加链接的功能。

至于您的第三条评论,如果您希望能够编辑文本区域中的原始文本,我建议您在将其插入数据库之前不要使用 nl2br。相反,保存原始文本,并且在向页面显示数据时仅使用 nl2br,因此..每当你回应它时。