用php

时间:2018-08-01 09:45:08

标签: php html html-table

我有问题。

因此,我正在尝试编写一个脚本,以用'tr''td'等标签替换所有HTML标记'div' strong>或“表格”

我必须使用其他函数PHP吗?我有几个HTML表,就像这样,我必须将所有脚本转换成一个好的表。

因此,如果您对我有个好主意,那将是很棒的:(谢谢!

我的代码:

$replace = array(
'<div style="display: table; width: 100%; border-spacing: 2px;">' => "<table style ='width: 100%; border-spacing: 2px;'>",
'<div style="display:table;width:100%;border-spacing: 2px;">' => "<table style ='width: 100%; border-spacing: 2px;'>",

'<div style="display: table-row; background: #dcdcba; color: #fff;">' => "<tr style='background: #dcdcba; color: #fff;'>",
'<div style="display: table-row;background: #dcdcba;color: #FFF;" >' => "<tr style='background: #dcdcba; color: #fff;'>",

'<div style="display: table-cell; padding: 6px 12px;">' => "<th style='padding: 6px 12px;'>",
'<div style="display: table-cell; padding:  6px 12px">' => "<th style='padding: 6px 12px;'>",

'<div style="display: table-row;">' => "<tr>",

'<div style="display: table-cell; padding: 6px 12px; background-color: #eee;">' => "<td style='padding: 6px 12px; background-color: #eee;'>",
'<div style="display: table-cell; padding:  6px 12px;background-color: #eee">' => "<td style='padding: 6px 12px; background-color: #eee;'>",

'</div>' => "</td>");


$newphrase = str_replace(array_keys($replace), $replace,  $value['description']);

错误的代码:

<div style="display: table; width: 100%; border-spacing: 2px;">
<div style="display: table-row; background: #dcdcba; color: #fff;">
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
</div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
</div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
</div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
</div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
</div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;"> LoremIpsum</div>
</div>

我的期望:

<table width: 100%; border-spacing: 2px;">
<th>
    <tr padding: 6px 12px;">LoremIpsum</tr>
    <tr padding: 6px 12px;">LoremIpsum</tr>
    <tr padding: 6px 12px;">LoremIpsum</tr>
    <tr padding: 6px 12px;">LoremIpsum</tr>
</th>

<tr>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
</tr>

<tr>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum1991</td>
    <td padding: 6px 12px; background-color: #eee;"> LoremIpsum</td>
</tr>

<tr>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;"> LoremIpsum</td>
</tr>

<tr>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum </td>
</tr>

<tr>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum</td>
    <td padding: 6px 12px; background-color: #eee;">LoremIpsum </td>
</tr>

1 个答案:

答案 0 :(得分:0)

因为您的问题最好使用PHP DOM库和更具体的DomDocument类。

下面我举一个例子,说明如何将div更改为表

$content = '
    <div style="display: table; width: 100%; border-spacing: 2px;">
    <div style="display: table-row; background: #dcdcba; color: #fff;">
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px;">LoremIpsum</div>
    </div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    </div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    </div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    </div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    </div>
    <div style="display: table-row;">
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum</div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;">LoremIpsum </div>
    <div style="display: table-cell; padding: 6px 12px; background-color: #eee;"> LoremIpsum</div>
    </div>';

// Create a new DOM Document and load your old HTML
$dom = new DOMDocument();
$dom->loadHTML($content);

// Create a new DOM Document
$newDom = new DOMDocument();

// Fetch all divs and append them to the new Document
$divs = $dom->getElementsByTagName('div');
$table = null;
$row = null;
foreach($divs as $div) {
    $attributeString = getAttributeString($div);
    $type = getDivType($attributeString);
    $attributes = getAttributeList($attributeString);
    if ($type === 'table') {
        $table = $newDom->createElement('table');
        foreach($attributes as $key => $value) {
            $table->setAttribute($key, $value);
        }
        $newDom->appendChild($table);
    } else if ($type === 'table-row') {
        if ($table) {
            $row = $newDom->createElement('tr');
            foreach($attributes as $key => $value) {
                $row->setAttribute($key, $value);
            }
            $table->appendChild($row);
        }
    } else if ($type === 'table-cell') {
        if ($row) {
            $cell = $newDom->createElement('td', $div->nodeValue);
            foreach($attributes as $key => $value) {
                $cell->setAttribute($key, $value);
            }
            $row->appendChild($cell);
        }
    }
}

// Get new HTML
echo $newDom->saveHTML();


/**
 * Get attribute list from attribute string
 */
function getAttributeList($attributeString)
{
    $list = [];
    $attributes = explode(';', $attributeString);
    foreach($attributes as $attribute) {
        $attributeParts = explode(':', $attribute);
        if (count($attributeParts) > 1 && $attributeParts[0] != 'display') {
            $list[$attributeParts[0]] = $attributeParts[1];
        }
    }

    return $list;
}

/**
 * Get attribute string from a div element
 */
function getAttributeString(DOMElement $div)
{
    foreach($div->attributes as $attribute) {
        if ($attribute->name == 'style') {
            return str_replace(' ', '', $attribute->value);
        }
    }

    return '';
}

/**
 * Get div type from its attribute string
 */
function getDivType($attributeString)
{
    $styles = explode(';', $attributeString);
    foreach($styles as $style) {
        $styleParts = explode(":", $style);
        if ($styleParts[0] === 'display') {
            return $styleParts[1];
        }
    }

    return null;
}

此代码将生成以下HTML

<table width="100%" border-spacing="2px">
   <tr background="#dcdcba" color="#fff">
      <td padding="6px12px">LoremIpsum</td>
      <td padding="6px12px">LoremIpsum</td>
      <td padding="6px12px">LoremIpsum</td>
      <td padding="6px12px">LoremIpsum</td>
   </tr>
   <tr>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum </td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
   </tr>
   <tr>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum </td>
   </tr>
   <tr>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum </td>
   </tr>
   <tr>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum </td>
   </tr>
   <tr>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum</td>
      <td padding="6px12px" background-color="#eee">LoremIpsum </td>
      <td padding="6px12px" background-color="#eee"> LoremIpsum</td>
   </tr>
</table>