如何使用php从单词中分割字符串

时间:2013-04-18 11:22:43

标签: php

我希望在font标签之间插入文本,并使用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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

返回:“你的文字”

答案 2 :(得分: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;
        }
    }
?>

答案 3 :(得分: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/>';
}