使用preg_replace从html中删除输入大小


remove input size from html with preg_replace

我使用的是Simple HTML DOM Parser,有了它,我可以从下面的对象片段中获取所有输入标记

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);
}

但没有成功。。。。

任何帮助都会通知。。。

这里没有理由使用正则表达式。您已经有了DOM,只需进行必要的操作:

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

你已经在输入标签中了,所以不需要去输入标签来搜索尺寸。使用这个preg,它会找到尺寸并将其删除

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

它肯定能在上运行