vekah
vekah

Reputation: 990

How to catch XML Dom with php regex

I got some XML Atom files on local. On all of those files, there are some <author>...</author> balise that I want to catch in a PHP string variable.

So I did that :

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>';
}

Files are opening, and strXML is the good string. I got some stranges behaviours of preg_match, that makes me think that this is not the good function... I got more balises than only the balises insides <author></author>

How should I do ?

Upvotes: 0

Views: 132

Answers (1)

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53603

  1. Don't try to reinvent the wheel.
  2. Regex is not good for parsing XML(just an example, not the absolute proof, but...)
  3. use the PHP built in tools like simple XML

Upvotes: 3

Related Questions