1

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

Basically, I have a table with several <td>'s in each <td> i have a<img>

I need to copy the width and height from the <img> and place it in the <td>

I have been doing this manually, however the number of <td>'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

6
  • do u make string concatenation any where? Commented Jul 12, 2011 at 15:38
  • 4
    this would likely be easier and more robust if you used the DOMDocument class rather than trying string manipulation. Commented Jul 12, 2011 at 15:39
  • 1
    Don't use a regular expression for html parsing... php has a couple nice xml parsers, I'm personally a fan of us2.php.net/manual/en/book.simplexml.php Commented Jul 12, 2011 at 15:39
  • 1
    How are you building this table? If it's via script, then have the script do the extraction/filling-in. Otherwise, use DOM to parse/process the html. Anything you can do with substring matching/regexes will be unreliable. Commented Jul 12, 2011 at 15:40
  • Why do you always people want to use regex? There are usually simpler and easier ways to reach your target! Commented Jul 12, 2011 at 15:46

3 Answers 3

1

You should consider using the DOMDocument class provided by PHP.

Example:

<?php

$doc = new DOMDocument;
$doc->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:

<html>
  <body>
    <table>
      <tr>
        <td><img src="test.png" height="100" width="100" /></td>
        <td>
          <p><img src="test2.png" height="100" /></p>
        </td>
      </tr>
    </table>
  </body>
</html>

To:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <body>
    <table>
      <tr>
        <td style="height:100px;width:100px;"><img src="test.png" height="100" width="100"></td>
        <td style="height:100px;">
          <p><img src="test2.png" height="100"></p>
        </td>
      </tr>
    </table>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks for your help, saved me a lot of head aches
0

Try something like this:

preg_replace('/<td>(.*?)<img(.*?)width=\"(\d+)(.*?)height=\"(\d+)(.*?)<\/td>/','<td width="${3}" height="${5}">${1}<img${2}width="${3}${4}height="${5}${6}</td>', $html);

switch width and height if necessary =)

Comments

0

You can do that without Regular Expressions. Try something like this:

var images = document.getElementsByTagName('img');
for(var i=0, j=images.length; i<j; i++){
    images[i].parentNode.setAttribute('height', images[i].height);
    images[i].parentNode.setAttribute('width', images[i].width);
}

The script assumes that you have no images other than you mentioned above.

4 Comments

The question is tagged php not javascript.
im getting "document.getElementsTagName is not a function" error on this one.
@DAVIEAC - It should be document.getElementsByTagName (it's missing the By).
I was looking for questions tagged regex when I got to this question and din't see the PHP tag. Sorry, for the tangle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.