-3

我想通过使用 php 删除所有标签来获取字体标签之间的文本。

<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">welcome</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">Test of Test asdasd</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">asdsadsadsadsad</FONT>
  </P>
</TEXTFORMAT>
4

4 回答 4

0

是的!让我们在 HTML 上做正则表达式。我预计至少有 10 票反对:D

<?php
    $string = '<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">welcome</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">Test of Test asdasd </FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">asdsadsadsadsad</FONT></P></TEXTFORMAT>';

    $result = preg_match_all("/<font.*?>(.*?)<\/font>/si", $string, $matches);

    if (count($matches) > 0) {
        unset($matches[0][0]);

        foreach($matches[0] as $key => $value) {
            echo $key.' >>> '.$value;
        }
    }
?>
于 2013-04-18T11:35:49.343 回答
0

使用这个“XML”:

$string = '<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">welcome</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0"></FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">Test of Test asdasd</FONT>
  </P>
</TEXTFORMAT>
<TEXTFORMAT LEADING="2">
  <P ALIGN="LEFT">
    <FONT FACE="Verdana" SIZE="10" COLOR="#0B333C"
    LETTERSPACING="0" KERNING="0">asdsadsadsadsad</FONT>
  </P>
</TEXTFORMAT>';

您可以使用strip_tags删除所有标签

echo strip_tags($string);

或者您可以使用simpleXML解析它:

$string = '<data>'.$string.'</data>';
$xml = simplexml_load_string($string);
foreach($xml as $t){
    echo $t->P->FONT.'<br/>';
}
于 2013-04-18T11:38:20.323 回答
0

如果您的 xml 始终是有效的 xml(html 肯定不是),您可以将其导入 DomDocument 并通过 Xpath 查询获取所有字体节点。

这样,您仍然可以拥有完整节点及其所有属性、父节点和子节点。

$doc = new DOMDocument($input);
$xpath = new DOMXPath($doc);

$query = '//FONT';

$entries = $xpath->query($query);

foreach ($entries as $entry)
    echo $entry->nodeValue;

http://www.php.net/manual/de/domxpath.query.php

于 2013-04-18T11:30:18.627 回答
0
$your_tagged_text = '<SOME TAGS VALUE="FOO"><FONT FACE="FONT">Your Text</FONT></SOME TAGS>';
$your_plain_text = strip_tags($your_tagged_text);

print $your_plain_text;

返回:“您的文本”

于 2013-04-18T11:30:28.687 回答