我已阅读 this post讨论将 html 中断标记转换为 php 中的新行。其他人说这对他们有用,但我发生了一些奇怪的事情。
这是我使用的代码:
$breaks = array("<br />", "<br>", "<br/>");
$jawaban = str_ireplace($breaks, " ", $jawaban1);`
这是他们使用的代码:
$breaks = array("<br />", "<br>", "<br/>");
$text = str_ireplace($breaks, "\r\n", $text);
如果有任何以前的帖子/PHP 方法让我知道
编辑:添加我的代码来回显文本框
<-- THIS WONT WORK -->
$username = $_SESSION['username'];
$unsafenomorsoal = $_POST['nomorsoal'];
$unsafejawaban = $_POST['jawaban'];
$nomorsoal = mysqli_real_escape_string($konek,$unsafenomorsoal);
$jawabannotcut = substr($unsafejawaban,0,50000);
$unsafejawabanfirst = nl2br($jawabannotcut);
$jawaban1 = mysqli_real_escape_string($konek,$unsafejawabanfirst);
$breaks = array("<br />","<br>","<br/>");
$jawaban = str_ireplace($breaks, PHP_EOL, $jawaban1);
$_SESSION['textvaluejawaban'] = $jawaban;
这就是 echo :
echo "<div class=\"head-main-recent-background\" style=\"background:white;width:99%;color:black;text-align:left;height:1000px;position:relative;top:130px;margin-top:10px;\">- Jawab Soal -<br/>".$jawabanerror."<br/>Nama : ".$_SESSION['username']."<br/>
<form method=\"post\" action=\"prosesjawabsoal.php\">
<input type=\"hidden\" name=\"nomorsoal\" value=\"".$_SESSION['nomorsoal']."\"/>
Jawaban : <br/>
<textarea placeholder=\"Max 40.000 Huruf\" style=\"overflow- x:none;width:99%;height:300px;\" type=\"text\" name=\"jawaban\" maxlength=\"40000\" >".$_SESSION['textvaluejawaban']."</textarea>
<br/>Captcha <br/>
<div style=\"overflow:hidden;\" class=\"g-recaptcha\" data- sitekey=\"6LfYQicTAAAAAFstkQsUDVgQ60x_93obnKAMKIM9\"></div><br/>
<button type=\"submit\" name=\"submit\" style=\"margin-top:10px;height:auto;width:auto;\">Kirim Jawaban</button>
</form>
</div>";
注意:该代码段将不起作用,因为它是 php
抱歉,我在发布代码时由于错误而使用了代码段!
编辑:
尝试了 preg_replace() 方法但结果仍然相同
编辑:
更改标题以告知 preg_replace 不起作用
最佳答案
你的问题是 mysqli_real_escape_string()。将“\r\n”转换为字符串以使其安全地输入到数据库中。完全删除它。当您输出到屏幕时,请改用 htmlspecialchars
:
echo htmlspecialchars($myUnsafeVar);
应用这些规则(作为起点,总是有可能的异常(exception),但在极少数情况下):
- 在将字符串输入数据库时使用
mysqli_real_escape_string
。它不会在输出到屏幕时执行您期望的操作 - 因此任何经过 mysql 转义() 的内容都不应出现在屏幕上。 - 在输出到屏幕时使用
htmlspecialchars
(你没有!)。 - 使用
url_encode
将内容添加到 URL 中 - 还有许多不同的“转义”功能(例如插入到JSON、插入到mysql、插入到其他数据库)。根据您的需要使用正确的 - 不要将其用于其他目的。
查看函数以获取更多详细信息。
就目前的情况来看,即使经过所有这些努力,您的代码仍不安全 - 但修复起来真的很简单!
关于php - str_ireplace 或 preg_replace 将中断标记替换为\r\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514698/