slow php DOMNode manipulation breaks
<p>I have a script that takes some HTML and tries to extract some data from it. The data I'm working from has the field name wrapped in a span.CardTitle, with the data in the text following. Unfortunately all fields and data are siblings of each other making it difficult to extract. this is my current script (shortened to relevant points):</p>

<pre><code>$time = microtime(true);

$curr_card = array();
$item = $list-&gt;item($i);
$cardPath = getHTML($base . $item-&gt;getAttribute('href'));

$time = microtime(true) - $time;
echo 'Time to download and load card info: ' . $time . '&lt;br /&gt;';

$title = $cardPath-&gt;evaluate('//div[@class=\'WordSection1\']/h4')-&gt;item(0)-&gt;textContent;
preg_match('/\s\(([A-Za-z0-9]+)\)/', $title, $curr_set);
$curr_card['set'] = $curr_set[1];
$curr_card['card_name'] = preg_replace('/\s\([A-Za-z0-9]+\)/', '', $title);

echo 'Getting field data for ' . $curr_card['card_name'] . '&lt;br /&gt;';

$fields = $cardPath-&gt;evaluate('//div[@class=\'WordSection1\']/p[@class=\'Definition\']/span[@class=\'CardTitle\']');

$time = $field_time = microtime(true);
echo '# of fields: ' . $fields-&gt;length . '&lt;br /&gt;';

for($a = 0; $a &lt; $fields-&gt;length; $a++)
{
    $field = $fields-&gt;item($a);

    $fieldName = $field-&gt;textContent;
    echo 'Field Name: ' . $fieldName . '&lt;br /&gt;';

    $fieldData = recursiveSibling($field-&gt;nextSibling);
    echo 'Field Data: ' . $fieldData . '&lt;br /&gt;';

    $field_time = microtime(true) - $field_time;
    $fieldnum = $a + 1;
    echo 'Field #' . $fieldnum . ' took ' . $field_time . ' to process. &lt;br /&gt;';

$field_time = microtime(true);
}
$time = microtime(true) - $time;
echo 'Time to extract card info: ' . $time . '&lt;br /&gt;';

function getHTML($url, $xpath = true)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Firefox (WindowsXP) – Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $html = curl_exec($ch);
    if (!$html) {
        echo "&lt;br /&gt;cURL error number:" .curl_errno($ch);
        echo "&lt;br /&gt;cURL error:" . curl_error($ch);
        exit;
    }
    if($xpath)
    {
        $dom = new DOMDocument();
        @$dom-&gt;loadHTML($html); 
        return new DOMXPath($dom);
    }
    else
        return $html;
}

function recursiveSibling($node)
{
    if(strstr($node-&gt;nodeName, 'span') === false)
    {
        $text = $node-&gt;textContent . recursiveSibling($node-&gt;nextSibling);
        return $text;
    }
}
</code></pre>

<p>This is as far as the script will output:</p>

<pre><code>Time to download and load master list: 0.495495080948
Time to download and load card info: 0.106231927872

Getting field data for A Child is Born
# of fields: 9

Field Name: Type: 
Field Data: Hero Enh. Â• 
Field #1 took 3.60012054443E-5 to process. 

Field Name: Brigade: 
Field Data: White Â• 
Field #2 took 1.00135803223E-5 to process. 

Field Name: Ability: 
Field Data: None Â• 
Field #3 took 8.10623168945E-6 to process. 

Field Name: Class: 
Field Data: None Â• 
Field #4 took 7.15255737305E-6 to process. 

Field Name: Special Ability: 
Field Data: Discard all Demons in Play. Cannot be interrupted, negated, or prevented. Â• 
Field #5 took 3.31401824951E-5 to process. 

Field Name: Errata: 
Field Data: Discard all demons in play. Cannot be negated. Â• 
Field #6 took 1.50203704834E-5 to process. 

Field Name: Identifiers: 
Field Data: None Â• 
Field #7 took 6.91413879395E-6 to process. 

Field Name: Verse: 
Field Data: None Â• 
Field #8 took 5.96046447754E-6 to process. 

Field Name: Availability: 
</code></pre>

<p>I don't understand why it takes so long (approx 40 sec) to execute, and I understand even less why the last field breaks the script. In case it helps, this is the page i'm extracting from : <a href="http://www.redemptionreg.com/REG/Master/achildisbornp.htm" rel="nofollow">http://www.redemptionreg.com/REG/Master/achildisbornp.htm</a></p>

<p>I would greatly appreciate it if anyone could explain to me what I'm doing wrong, and how to make it faster. there are over 2000 cards to do this to, and at 45 sec each, that's over 24 hour script execution!</p>
