2012-09-14 51 views
1

我使用簡單的HTML DOM解析器,並用這個我能得到所有的輸入標籤的對象片斷如下從HTML用了preg_replace

foreach ($InputObj->find('input') as $e) { 

    $inputTag = $e->outertext; 

    // now I want to check if input element have size attribute then remove it with preg_replace 
    $inputTagsSizeStrip = preg_replace('~\<input[^\s]*size=\'|\"[^\'|\"]~is', "" , $inputTag); 
} 

,但沒有成功刪除輸入大小....

任何幫助將appriciated ...

回答

4

沒有理由在這裏使用正則表達式。你已經有DOM,只是做了必要的操作:

foreach ($InputObj->find('input') as $e) { 
    if ($e->hasAttribute('size')) { 
    $e->removeAttribute('size'); 
    } 
} 
+0

唯唯諾諾的人其工作謝謝,我不知道簡單的解析器有很多感謝的能力一噸 –

1

你已經在輸入標籤所以沒有必要去輸入標籤來搜索大小使用這種浸漬料會找到它的大小,將其取下

foreach ($InputObj->find('input') as $e) { 
     $inputTagsSizeStrip = preg_replace('~(size=(\"|\')[^\'|\"]*(\"|\'))~is', $changeSrc , $inputTagsSizeStrip); 
} 

它肯定會工作

+0

它也可以謝謝,但我接受肖恩光明的答案,因爲他做到了簡單的html解析器 –