php 数组 strip_tags 并取消设置

发布于 2024-12-19 07:13:29 字数 1601 浏览 3 评论 0原文

大家好,我有一个关于 strip_tags 函数的问题。 我有一个这样的html文档。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>er</p>
</body>
</html>

这个php脚本的

<?php
$file = "ht.html";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
$output = str_replace("\t|\t", "|", $data);
$outputline = explode("\n", $output);
$lexeisline=count($outputline);

for ($i = 0; $i < $lexeisline; $i++){
    $outputline[$i]=strip_tags($outputline[$i]);
      if (empty($outputline[$i])){
          unset($outputline[$i]);
}
}
$outputline = array_values($outputline);
$lexeisline=count($outputline);
echo "<p>";
for ($i = 0; $i < $lexeisline; $i++){
echo ($outputline[$i])."<br />";
}
echo "</p>";
?>

问题是它不会取消设置空变量(从strip_tags返回)并回显类似这样的内容。以下是否意味着它回显空字符串? 任何意见或帮助将不胜感激。提前感谢

<p>
<br />
<br />
<br />
<br />Untitled Document
<br />
<br />
<br />er
<br />
<br /></p>

@phpmeh

 Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
        [4] => Untitled Document
        [5] => 
        [6] => 
        [7] => er
        [8] => 
    )

hello everybody i have a question regarding strip_tags function.
i have an html document like that.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>er</p>
</body>
</html>

and this php script

<?php
$file = "ht.html";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
$output = str_replace("\t|\t", "|", $data);
$outputline = explode("\n", $output);
$lexeisline=count($outputline);

for ($i = 0; $i < $lexeisline; $i++){
    $outputline[$i]=strip_tags($outputline[$i]);
      if (empty($outputline[$i])){
          unset($outputline[$i]);
}
}
$outputline = array_values($outputline);
$lexeisline=count($outputline);
echo "<p>";
for ($i = 0; $i < $lexeisline; $i++){
echo ($outputline[$i])."<br />";
}
echo "</p>";
?>

the problem is that it does not unset the empty vars(which are returned from the strip_tags) and echos something like this. does the following means that it echos empty strings?
any opinion or help will be very appreciated. Thanx in advance

<p>
<br />
<br />
<br />
<br />Untitled Document
<br />
<br />
<br />er
<br />
<br /></p>

@phpmeh

 Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
        [4] => Untitled Document
        [5] => 
        [6] => 
        [7] => er
        [8] => 
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呢古 2024-12-26 07:13:29

你只是逐一数数并回响。如果您想跳过空,请在循环中执行以下操作:

for ($i = 0; $i < $lexeisline; $i++){
if(!empty($outputline[$i]) echo ($outputline[$i])."<br />";
}

希望我正确理解您的意思。

You're just counting through and echoing for each. If you want to skip empty, do something like this in your loop:

for ($i = 0; $i < $lexeisline; $i++){
if(!empty($outputline[$i]) echo ($outputline[$i])."<br />";
}

Hope I understood you correctly.

百思不得你姐 2024-12-26 07:13:29

对于您正在做的事情来说,这段代码看起来非常巴洛克。下面的代码能满足您的需要吗?

$lines = file('ht.html');
$lines = array_map('strip_tags', $lines);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);

echo "<p>\n", implode("<br />\n", $lines), "\n</p>";

This code seems incredibly baroque for what you are doing. Does the following code do what you need?

$lines = file('ht.html');
$lines = array_map('strip_tags', $lines);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);

echo "<p>\n", implode("<br />\n", $lines), "\n</p>";
寄风 2024-12-26 07:13:29

我可能是错的......但是......

当你分解这些行时,我很确定它们有一个隐式的 /n 这意味着它们永远不会为空。另一种选择是:

 strlen( $outline[$i] ) > 0 

I'm probably wrong... but...

When you explode these lines, I'm pretty sure they have an implicit /n that is going to mean they're never empty. An alternative, though it is a little slower, would be:

 strlen( $outline[$i] ) > 0 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文