添加自动换行符和超链接到textarea字段?

时间:2013-11-15 19:14:18

标签: php

我有一个Joomla网站使用一些简单的php来提交表单,但我希望能够在输入我的文本时按Enter键自动添加换行符,以及自动转换任何网址进入可点击的链接。这是可能的,任何人都可以告诉我怎么样?

    <?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);
?>

1 个答案:

答案 0 :(得分:0)

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

filter_var($yourstring, FILTER_VALIDATE_URL);

就可以了。如果它返回true,则只需在其周围添加html链接标记并替换数组元素。最后,破坏你的数组以获得用于存储在数据库中的明文。

Here is a function for adding link I found with quick google search.

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