表单提交在表单的开头文本前添加了一个<br/>

时间:2012-09-13 15:19:09

标签: php sql-server

好的,我正在使用PHP编辑表单(之前建立的人员)将数据提交到MSSQL数据库中,但是当信息提交到数据库中并且稍后检索表单页面(用于编辑)时, <br>在文本开头。如果字段以<ul>开头,但未插入<br>。我一直试图弄清楚如何制止这一点。我试过的一件事最后删除了文本中的所有<br>,这使得格式化消失了,我怎样才能防止它在顶部插入<br>但在其他地方保留<br>?这是我的代码:(我给你一切到我出现问题的第一个字段)

function fromhtml ($x) {
  $x = preg_replace("/<p>/i","\n\n",$x);
  //$x = preg_replace("/<br>/i","\n",$x);
  $x = preg_replace("/<li>/i","\n<li>",$x);

  return $x;
}

$PHP_SELF = $_SERVER['PHP_SELF'];


$course_id = @$_GET["course"];

if ($course_id == "") {

$sqlquery = "SELECT id, title, goals, outline, reference, deliverymode, updated2 = CONVERT(VARCHAR(19), updated, 120)
FROM courses WHERE division_id = '$division_id' ORDER by id";


$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);

print "<table border=1 id=\"content_table\"><tr><th>ID</th><th>Title</th><th>Status</th><th>Last modified</th></tr>\n";

$i = 0;
while ($number > $i) {

        $course_id =  mssql_result($result,$i,"id");
    $reference = mssql_result($result,$i,"reference");
    $updated = mssql_result($result,$i,"updated2");

    print "<tr><td>";
    if ($reference == "") {
            print "<a href=$PHP_SELF?division=$division_id&course=$course_id>$course_id</a>";
    } else {
            print "$course_id";
    }
    print "</td><td>";
        print mssql_result($result,$i,"title");
    print "</td><td>";
    if ($reference == "") {
        if ( (mssql_result($result,$i,"goals")=="") and (mssql_result($result,$i,"outline")=="") ) {
            print "<b>No syllabus, or incomplete</b></td>";
        }
    } else {
        print "Based on $reference";
    }
    print "</td><td>$updated</td>\n";
        print "</tr>\n";  
        $i++;
}

print "</table>";

    exit;
}

$sqlquery = "SELECT * FROM courses WHERE id = '$course_id'";

$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);

if ($number == 0) {
print "<html><body>";
print "No course with the ID \"$course_id\" exists in the course database.";
print "</body></html>";
exit;
}

$i = 0;

$description = fromhtml(mssql_result($result,$i,"description"));
print "<html><head><title>DACC Course Syllabus - $course_id</title>";

print "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/rte/richtext2.js\"></script>";
?>

<script language="JavaScript" type="text/javascript">
<!--    
initRTE("/rte/images/", "/rte/", "");
//-->   

function submitForm() {
   updateRTEs();
   document.edit-course.submit();
   return false;
}
</script>
<?php   
print "</head><body>\n";

//Begin Form
print "<form name=\"edit-course\" id=\"edit-form\" method=\"post\" action=\"write.php?course=$course_id\" onSubmit=\"return submitForm()\">\n";


print "<fieldset>";
 print "<label for=\"description\">Course Description</label>";
$description=addslashes(preg_replace('`[\r\n]`','',$description));
  ?>
   <script language="JavaScript" type="text/javascript">
<!--
  writeRichText('description', '<?php print $description; ?>', 200, 300, true, false);
//-->
</script>
  <?php

   print "</fieldset>";

2 个答案:

答案 0 :(得分:0)

preg_replace有第四个参数$limit。将此值设置为1会将preg_replace限制为第一次出现。或者,您可以使用正则表达式^<br>。这只会在字符串开头处替换<br>

编辑:对不起,我误解了你的问题。使用$variable=preg_replace("/^<br>/i", "", $variable);删除第一个<br>

答案 1 :(得分:0)

BR处于字符串的最开头,然后使用此

$x = preg_replace("/^<br(\/|)>/i","\n",$x);

而不是注释行