2012-06-04 76 views
0

所以可以說我有:PHP獲得元素

<?php 
    $template = '<img src="{image}" editable="all image_all" />'; 
    $template .= '<div>'; 
    $template .= '<img src="{image}" editable="yes" />'; 
    $template .= '</div>'; 
?> 

現在我想的是使腳本通過所有包含{圖像} src和檢查,看看是否有任何元素其中有

editable="all" 

屬性。

如果是這樣:獲取第二個可編輯屬性,例如

image_all 

並將其包含到src中。

+1

你有沒有看着PHP的DOM解析器? http://simplehtmldom.sourceforge.net/ –

+1

嘗試[簡單的HTML DOM分析器](http://simplehtmldom.sourceforge.net/) – Jashwant

+0

使用PHP [文檔對象模型](http://www.php.net/manual /en/book.dom.php)。它會很好地爲你想要做的事情工作。 – AustinAllover

回答

1

這個任務可以通過使用庫來簡化建議的意見,Simple HTML DOM Parser

正是因爲這很容易:

$images = array(); //an array for your images with {image} in src 
$html = "..."; 
foreach($html->find('img') as $element) 
    if($element->src == '{image}') { 
     //add to the collection 
     $images[] = $element; 
    } 
    //Also you can compare for the editable attribute same way as above. 
} 
0
如果你想獲得第二個可編輯ATTR,並將其保存

像$ SRC的數組,以便檢查這個代碼:

$content=new DOMDocument(); 
$content->loadHTML($template); 
$elements=simplexml_import_dom($content); 
$images=$elements->xpath('//img'); 
foreach ($images as $img) { 
    if(preg_match('/all /i', $img['editable'])) 
     $src[]=substr($img['editable'],4) ; 
} 
print_r($src); 

將輸出:

Array ([0] => image_all) 
0

試試這個,

include('simple_html_dom.php'); 
    $html = str_get_html('<div><img src="{image}" editable="all image_all" /><img src="{image}" editable="yes" /></div>'); 
    $second_args= array(); 
    foreach($html->find('img[src="{image}"]') as $element){ 
    $editables = explode(' ',$element->editable); 
    if($editables[0] === "all"){ 
     $second_args[] = $editables[1]; 
    } 
    } 
    print_r($second_args);