0

I want to replace all td's with th's for only the first tr. But I cannot really figure out how to do this using Regex in PHP. (replace the td at all)

     <tr class="row0 row-hover">
        <td class="column-0 "><?php _e('Data', 'domain'); ?> </td>
        <td class="column-0 ">USD</td>
        <td class="column-0 ">EUR</td>
        <td class="column-0 ">GBP</td>
        <td class="column-0 ">CAD</td>
        <td class="column-0 ">CHF</td>
        <td class="column-0 ">HKD</td>
        <td class="column-0 ">AUD</td>
        <td class="column-0 ">SGD</td>
        <td class="column-0 ">SEK</td>
        <td class="column-0 ">NOK</td>
        <td class="column-0 ">CNH</td>
      </tr>

First of all I have there markup. I try to match them using <tr.*\(<td>).*\. But that don't seem to work. I know that I have to firstly match on the (.*). But I cannot get the td's.

The second thing would be use the preg_replace replace function. I saw that you can set here an $limit. So that would be just one? And what is the best way to group this for the preg_replace function?

It would be nice if it becomes this :)

         <tr class="row0 row-hover">
            <th class="column-0 "><?php _e('Data', 'domain'); ?> </th>
            <th class="column-0 ">USD</th>
            <th class="column-0 ">EUR</th>
            <th class="column-0 ">GBP</th>
            <th class="column-0 ">CAD</th>
            <th class="column-0 ">CHF</th>
            <th class="column-0 ">HKD</th>
            <th class="column-0 ">AUD</th>
            <th class="column-0 ">SGD</th>
            <th class="column-0 ">SEK</th>
            <th class="column-0 ">NOK</th>
            <th class="column-0 ">CNH</th>
          </tr>

Daansk44
  • 497
  • 2
  • 4
  • 21

1 Answers1

0

No regex involved, but its not required anyways:

https://3v4l.org/0n9Af

<?php

$input = <<<EOT
<tr class="row0 row-hover">
        <td class="column-0 "><?php _e('Data', 'domain'); ?> </td>
        <td class="column-0 ">USD</td>
        <td class="column-0 ">EUR</td>
        <td class="column-0 ">GBP</td>
        <td class="column-0 ">CAD</td>
        <td class="column-0 ">CHF</td>
        <td class="column-0 ">HKD</td>
        <td class="column-0 ">AUD</td>
        <td class="column-0 ">SGD</td>
        <td class="column-0 ">SEK</td>
        <td class="column-0 ">NOK</td>
        <td class="column-0 ">CNH</td>
      </tr>
<tr class="row0 row-hover">
        <td class="column-0 "><?php _e('Data', 'domain'); ?> </td>
        <td class="column-0 ">USD</td>
        <td class="column-0 ">EUR</td>
        <td class="column-0 ">GBP</td>
        <td class="column-0 ">CAD</td>
        <td class="column-0 ">CHF</td>
        <td class="column-0 ">HKD</td>
        <td class="column-0 ">AUD</td>
        <td class="column-0 ">SGD</td>
        <td class="column-0 ">SEK</td>
        <td class="column-0 ">NOK</td>
        <td class="column-0 ">CNH</td>
      </tr>
EOT;


$delimiter = '</tr>';
$content = substr($input, 0, strpos($input, $delimiter) + strlen($delimiter));
$output = str_replace("<td", "<th", $content);
$output .= substr($input, strpos($input, $delimiter) + strlen($delimiter));

  1. Set a delimiter, in this case </tr>
  2. Get the content up to (including) the first </tr>
  3. Replace every occurence of <td with <th
  4. Append the remaining content back after the transformed content
Xatenev
  • 6,383
  • 3
  • 18
  • 42