0

好的,我正在使用 PHP 编辑表单(以前的人构建)将数据提交到 MSSQL 数据库,这工作正常但是当信息提交到数据库并稍后检索表单页面(用于编辑)<br>时,开头有一个的文本。如果字段以未插入的 a<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>";
4

2 回答 2

0

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

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

于 2012-09-13T15:34:21.870 回答
0

BR 位于字符串的最开头,然后使用它

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

而不是那个注释行

于 2012-09-13T15:35:14.767 回答