0

我在本地获得了一些 XML Atom 文件。<author>...</author>在所有这些文件中,我想在 PHP 字符串变量中捕获一些应答器。

所以我这样做了:

function parseAtomByBalise($xml,$balise) {
    $arrayStr=array();
    preg_match('#<'.$balise.'>(.*)</'.$balise.'>#',$xml,$arrayStr);

    return $arrayStr;
}
 $fxml=fopen($xml,'r');
    $strXML=fgets($fxml);
    echo '<p>author: <textarea>';
    $authors=parseAtomByBalise($strXML,'author');
    foreach($authors as $author) {
        if($author!=$strXML)
            echo $author.'\n';
    }
    echo '</textarea></p>';
}

文件正在打开,strXML 是好的字符串。我有一些 preg_match 的奇怪行为,这让我觉得这不是好的功能......我得到的应答器比应答器内部的要多<author></author>

我应该怎么做 ?

4

1 回答 1

3
  1. 不要试图重新发明轮子。
  2. Regex不适合解析 XML(只是一个例子,不是绝对的证明,但是......)
  3. 使用简单的 XMLPHP等内置工具
于 2013-01-29T16:49:47.523 回答