2016-08-30 68 views
0

我試圖解析如下代碼:解析HTML元素插入部分

<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable" style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important;"> 

基本上我需要的是:

Array 
(
    [0] => <table 
    [1] => border="0" 
    [2] => cellpadding="0" 
    [3] => cellspacing="0" 
    [4] => height="100%" 
    [5] => width="100%" 
    [6] => id="bodyTable" 
    [7] => style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important; 
) 

但是用一個簡單的explode(" ", $str);這樣做時,我得到「風格」也分解了,有沒有一種簡單的方法來解析並循環通過它?

+0

如果你正在使用的空間在'爆炸)分隔符('從'style'屬性中刪除空格。 – coderodour

+0

如果你只是在尋找一個快速的hacky解決方案,你也可以嘗試正則表達式。 – dan08

+0

http://php.net/manual/en/function.explode.php – Aaron

回答

0

試試這個:

$result = array(); 
$str = '<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable" style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important;">'; 
$str_arr = explode (' style', $str); 
$result = explode (' ', $str_arr[0]); 
array_push($result, 'style'.$str_arr [1]); 

echo '<pre>'; 
print_r($result);