PHP Find and replace function

I need a regular expression find and replace that I’m a little stuck on.

Basically, I have a table with several


‘s in each


i have a

I need to copy the width and height from the
and place it in the

I have been doing this manually, however the number of


‘s is growing and its taking to long.

I just need help doing the actual find and replace

any ideas?

— EDIT —

Thanks for the advice, Think I will go down the dom route as I know it better. I have done reg ex before which did a task which was a much more simple idea so just went from there. You’ve saved me some time

You should consider using the
DOMDocument

class provided by PHP.

Example:

loadHTMLFile('/path/to/file.html');

// Find the TD elements.
$tds = $doc->getElementsByTagName('td');

// If TD elements were found, loop through each one of them.
if ( ! empty($tds) )
  foreach ( $tds as $td )
  {
    // Find the IMG elements located inside that TD
    $imgs = $td->getElementsByTagName('img');

    // Find the style attribute of the TD just in case one already exists.
    $style = $td->getAttribute('style');

    // I'm not sure what to do if multiple images are found so instead of looping to many, make sure only 1 is found.
    if ( ! empty($imgs) && count($imgs) == 1 )
    {
      $height = $imgs->item(0)->getAttribute('height');
      $width = $imgs->item(0)->getAttribute('width');

      if ( ! empty($height) )
        $style .= 'height:' . $height . 'px;';

      if ( ! empty($width) )
        $style .= 'width:' . $width . 'px;';

      // Update the style attribute of the TD element.
      $td->setAttribute('style', $style);
    }
  }

// Save the HTML document.
$doc->saveHTMLFile('/path/to/newfile.html');

Updates:

  
    

To:


  
    

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » PHP Find and replace function

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录