substr 行为怪异 (PHP)
substr acts wierd (PHP)
首先我php见识低
我的问题是 substr 无法正常工作,所以我需要帮助来理解原因。
我已经删除了 style.css,我得到了相同的结果(没有样式的 ofc)。
这是我得到的代码和结果图片。
我尝试了一个函数,得到了相同的结果。
我错过了一些东西,我不知道是什么。
我从 php 中删除了 <tr> <td>
并尝试使用 html 但我得到了相同的结果。
我只想显示 $res['descriere']
的前 100 个字符,当我尝试查看它时显示所有字符。
我以前这样做过,但现在不行了:(
感谢您的宝贵时间
substr结果图片:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM products WHERE login_id=".$_SESSION['dep_id']." ORDER BY id DESC");
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['trn_date']."</td>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['locatia']."</td>";
echo "<td>".$res['canal_primire']."</td>";
echo "<td>".$res['intemeiata']."</td>";
echo "<td>".$res['domeniu_vizat']."</td>";
echo "<td>".substr($res['descriere'],0,100)."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Editați</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Ești sigur că vrei să ștergi?')\">Șterge</a>| <a href=\"view_id.php?id=$res[id]\">Vizualizați</a></td>";
echo "</tr>";
}
?>
根据我对您语言的有限理解,descriere
似乎翻译成 "description"。你的图片显示了一个被破坏的HTML table,这意味着描述中必须有HTML(一个table)。
您基本上是在网页上的 table 中打印 HTML table 的子字符串。由于内部 table 无效 HTML,浏览器会尝试理解它。这就是你得到的。
要消除奇怪的结果,您需要在截断代码后关闭代码中所有打开的 HTML 标记。这可以通过以下功能实现:
function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
来源:Close open HTML tags in a string
首先我php见识低
我的问题是 substr 无法正常工作,所以我需要帮助来理解原因。
我已经删除了 style.css,我得到了相同的结果(没有样式的 ofc)。
这是我得到的代码和结果图片。
我尝试了一个函数,得到了相同的结果。
我错过了一些东西,我不知道是什么。
我从 php 中删除了 <tr> <td>
并尝试使用 html 但我得到了相同的结果。
我只想显示 $res['descriere']
的前 100 个字符,当我尝试查看它时显示所有字符。
我以前这样做过,但现在不行了:(
感谢您的宝贵时间
substr结果图片:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM products WHERE login_id=".$_SESSION['dep_id']." ORDER BY id DESC");
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['trn_date']."</td>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['locatia']."</td>";
echo "<td>".$res['canal_primire']."</td>";
echo "<td>".$res['intemeiata']."</td>";
echo "<td>".$res['domeniu_vizat']."</td>";
echo "<td>".substr($res['descriere'],0,100)."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Editați</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Ești sigur că vrei să ștergi?')\">Șterge</a>| <a href=\"view_id.php?id=$res[id]\">Vizualizați</a></td>";
echo "</tr>";
}
?>
根据我对您语言的有限理解,descriere
似乎翻译成 "description"。你的图片显示了一个被破坏的HTML table,这意味着描述中必须有HTML(一个table)。
您基本上是在网页上的 table 中打印 HTML table 的子字符串。由于内部 table 无效 HTML,浏览器会尝试理解它。这就是你得到的。
要消除奇怪的结果,您需要在截断代码后关闭代码中所有打开的 HTML 标记。这可以通过以下功能实现:
function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
来源:Close open HTML tags in a string