【问题标题】:HTML tags not shown in result captured from PHP regular expression从 PHP 正则表达式捕获的结果中未显示 HTML 标记
【发布时间】:2016-12-05 20:21:26
【问题描述】:

我正在尝试提取 <HostProperties> 标记内的所有内容。我在 python 中使用这个正则表达式,它工作正常,但是当我在 PHP 中使用这个正则表达式时,它会忽略 <HostProperties> 标签内的所有其他标签,并给出如下所示的输出。

我的输入是:

<HostProperties>
<tag name="cpe-1">cpe:/a:microsoft:iis:8.5</tag>
<tag name="HOST_END">Fri May  6 07:15:32 2016</tag>
<tag name="LastUnauthenticatedResults">1462533332</tag>
<tag name="Credentialed_Scan">false</tag>
<tag name="policy-used">Advanced Scan</tag>
<tag name="patch-summary-total-cves">16</tag>
<tag name="cpe">cpe:/o:microsoft:windows</tag>
<tag name="os">windows</tag>
<tag name="cpe-0">cpe:/o:microsoft:windows_server_2012:r2::datacenter</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">Microsoft Windows Server 2012 R2 Datacenter</tag>
<tag name="mac-address">34:40:b5:a1:be:8a</tag>
<tag name="traceroute-hop-0">172.23.144.5</tag>
<tag name="host-ip">172.23.144.5</tag>
<tag name="netbios-name">PKLHODC03</tag>
<tag name="HOST_START">Fri May  6 07:05:56 2016</tag>
</HostProperties>

我的 PHP 正则表达式代码:

 preg_match_all("#(?<=(<HostProperties>))((?:.|\n)*?)(?=</HostProperties)#s", $reading, $host_properties);


                    echo "<pre>";

                    var_dump($host_properties[2]);

                    echo "</pre>";

                    echo "<br>";

我的输出是:

cpe:/a:microsoft:iis:8.5
Fri May  6 07:15:32 2016
1462533332
false
Advanced Scan
16
cpe:/o:microsoft:windows
windows
cpe:/o:microsoft:windows_server_2012:r2::datacenter
general-purpose
Microsoft Windows Server 2012 R2 Datacenter
34:40:b5:a1:be:8a
172.23.144.5
172.23.144.5
172.23.144.5
172.23.144.5
PKLHODC03
Fri May  6 07:05:56 2016

我的预期输出是:

 <tag name="cpe-1">cpe:/a:microsoft:iis:8.5</tag>
 <tag name="HOST_END">Fri May  6 07:15:32 2016</tag>
 <tag name="LastUnauthenticatedResults">1462533332</tag>
 <tag name="Credentialed_Scan">false</tag>
 <tag name="policy-used">Advanced Scan</tag>
 <tag name="patch-summary-total-cves">16</tag>
 <tag name="cpe">cpe:/o:microsoft:windows</tag>
 <tag name="os">windows</tag>
 <tag name="cpe-0">cpe:/o:microsoft:windows_server_2012:r2::datacenter</tag>
 <tag name="system-type">general-purpose</tag>
 <tag name="operating-system">Microsoft Windows Server 2012 R2    Datacenter</tag>
 <tag name="mac-address">34:40:b5:a1:be:8a</tag>
 <tag name="traceroute-hop-0">172.23.144.5</tag>
 <tag name="host-ip">172.23.144.5</tag>
 <tag name="netbios-name">PKLHODC03</tag>
 <tag name="HOST_START">Fri May  6 07:05:56 2016</tag>

为什么我的输出与我的预期不同?

【问题讨论】:

  • 我认为你在那个输出上弄错了。但是尝试'~(?&lt;=&lt;HostProperties&gt;).*?(?=&lt;/HostProperties)~s' 并打印$host_properties[0]
  • 您是否在 Web 浏览器中查看输出?这些标签可能会被浏览器解释而不是显示出来,就像它们是 HTML 一样。
  • 使用"#&lt;HostProperties&gt;(.*?)&lt;/HostProperties&gt;#s"print_r($matches[1])。无需环顾四周。然后,考虑使用 DOM。
  • Faisal,下面显示的答案是否回答了您的问题?如果是这样,请记得accept it。这会向其他用户显示您找到了解决方案。

标签: php regex pcre


【解决方案1】:

&lt;pre&gt; 标签does not prevent markup from being rendered

注意:您需要将任何包含的“&amp;lt;”字符转义为“&amp;lt;”,以确保所包含的代码不会被解释为标记。

由于您使用的是 PHP,htmlspecialchars() 函数在这里可能很有用:

echo "<pre>";
echo htmlspecialchars($host_properties[2]);
echo "</pre>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多