Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php自动替换标记HTML_Php_Html_Html Table - Fatal编程技术网

用php自动替换标记HTML

用php自动替换标记HTML,php,html,html-table,Php,Html,Html Table,我有个问题 因此,我试图创建一个脚本,用类似于'tr'、'td'或'table'的标记替换所有标记HTML'div' 我必须使用PHP的另一个函数吗?我需要几个HTML表,就像这样,我必须转换所有的脚本以获得一个好的表 所以,如果你有一个想法给我,它将是伟大的:(谢谢 我的代码: $replace = array( '<div style="display: table; width: 100%; border-spacing: 2px;">' => "<table st

我有个问题

因此,我试图创建一个脚本,用类似于'tr''td''table'的标记替换所有标记HTML'div'

我必须使用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']);
$replace=array(
'' => "",
'' => "",
'' => "",
'' => "",
'' => "",
'' => "",
'' => "",
'' => "",
'' => "",
'' => "");
$newphrase=str_replace(数组_键($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>
LoremIpsum
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普1991
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆

对于您的问题,最好使用PHP库和更具体的类

下面我给你们一个例子,说明如何将div改为table

$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;
}
$content='1!'
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
';
//创建新的DOM文档并加载旧的HTML
$dom=新的DOMDocument();
$dom->loadHTML($content);
//创建新的DOM文档
$newDom=newdomdocument();
//获取所有div并将其附加到新文档中
$divs=$dom->getElementsByTagName('div');
$table=null;
$row=null;
foreach($div作为$div){
$attributeString=getAttributeString($div);
$type=getDivType($attributeString);
$attributes=getAttributeList($attributeString);
如果($type=='table'){
$table=$newDom->createElement('table');
foreach($key=>$value的属性){
$table->setAttribute($key,$value);
}
$newDom->appendChild($table);
}else if($type==‘表行’){
若有($表){
$row=$newDom->createElement('tr');
foreach($key=>$value的属性){
$row->setAttribute($key,$value);
}
$table->appendChild($row);
}
}else if($type==='table cell'){
如果($行){
$cell=$newDom->createElement('td',$div->nodeValue);
foreach($key=>$value的属性){
$cell->setAttribute($key,$value);
}
$row->appendChild($cell);
}
}
}
//获取新的HTML
echo$newDom->saveHTML();
/**
*从属性字符串获取属性列表
*/
函数getAttributeList($attributeString)
{
$list=[];
$attributes=分解(“;”,$attributeString);
foreach($attributes作为$attribute){
$attributeParts=分解(“:”,$attribute);
如果(计数($attributeParts)>1&&$attributeParts[0]!='display'){
$list[$attributeParts[0]]=$attributeParts[1];
}
}
返回$list;
}
/**
*从div元素获取属性字符串
*/
函数getAttributeString(doElement$div)
{
foreach($div->attributes as$attribute){
如果($attribute->name=='style'){
返回str_replace(“”,,$attribute->value);
}
}
返回“”;
}
/**
*从属性字符串中获取div类型
*/
函数getDivType($attributeString)
{
$styles=分解(“;”,$attributeString);
foreach($style作为$style){
$styleParts=分解(“:”,$style);
如果($styleParts[0]=='display'){
返回$styleParts[1];
}
}
返回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>

洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆
洛雷米普苏姆

这听起来像是一个问题。我可以问一下这个用例是什么吗?html来自哪里?看起来它是从数据库数据中呈现出来的。您最好在IDE中进行搜索和替换…(我希望您不想在生产时使用此选项)仅供参考:您的期望值无效HTMLXY问题令人担忧。我需要这样做,对于转换,在好的synthax表中转换坏的synthax表!正确转换所有div in标记。为什么我的期望值D无效?编辑后,预期的html更糟…+1!非常好,但我有一个问题,属性必须是“样式”,否则,该属性不起作用:/因此,有必要这样做:'我尝试:$list['style']=$attributeParts[0]。':“$attributeParts[1]”;但只有一个参数有效:/I我提出的解决方案是一个指导原则。你可以根据自己的喜好修改它,并使用样式和/或其他任何你喜欢的属性。我成功地做到了完美。小问题是,我的表格前有文本…有时甚至在…包含在标签中,它们甚至包含标签或
…我如何替换文本e确切地说,还是在旧文件中?非常感谢!