2016-01-30 21 views
0

我不想使用simplexml_load_file功能在我的PHP腳本,但我必須從一臺服務器的XML文件通過fsockopenfwritefgets功能的線。獲取從字符串的XML元素值不使用使用simplexml_load_file

如何從字符串(行)獲取XML元素值?

$fs = fsockopen("website", 80); 
if (!$fs) { 
    echo "$errstr ($errno)<br />\n"; 
} else { 
    fwrite($fs, $req.$bdy); 
    while(!feof($fs)) { 
     $GLOBALS['s'] = fgets($fs); 
    } 
} 
fclose($fs); 
$attr = $GLOBALS['s']->attributes(); // THIS DOES NOT WORK 
echo $attr['Day date']; // I CANNOT GET VALUE HERE 
+0

可以共享xml行和您想獲得什麼屬性? –

+1

'$ GLOBALS ['s']'只是一個字符串。使用DOMDocument解析它。 – PeeHaa

+0

@Orchidea你檢查了['simplexml_load_string()'](http://php.net/manual/en/function.simplexml-load-string.php)? – har07

回答

0

如果是字符串,則可以使用正則表達式來獲取屬性值。從問題我假定你需要「日期」屬性。爲了得到它,你可以使用簡單的正則表達式來獲取值。請參閱我的下面的代碼,

$fs = fsockopen("website", 80); 
if (!$fs) { 
    echo "$errstr ($errno)<br />\n"; 
} else { 
    fwrite($fs, $req.$bdy); 
    while(!feof($fs)) { 
     $GLOBALS['s'] = fgets($fs); 
    } 
} 
fclose($fs); 
// to get the matched attribute 
preg_match('/date\=\\"(.*?)\"/', $GLOBALS['s'], $output_array); 
var_dump($output_array[1]); 
+0

這個解決方案正在工作---我想,我可以通過這種方式從字符串中獲取所有屬性。謝謝! – Orchidea

+0

歡迎。只感謝? :) –