doutang3760 2016-02-26 02:34
浏览 16
已采纳

将DOM修改为样式顺序标题

Let's start with this html in my database table:

<section id="love">
<h2 class="h2Article">III. Love</h2>
<div class="divArticle">

This is what the display looks like after I run it through a DOM script:

<section id="love"><h2 class="h2Article" id="a3" data-toggle="collapse" data-target="#b3">III. Love</h2>
<div class="divArticle collapse in article" id="b3">

And this is what I would like it to look like this:

<section id="love"><h2 class="h2Article" id="a3" data- toggle="collapse" data-target="#b3">
    <span class="Article label label-primary">
        <i class="only-collapsed fa fa-chevron-down"></i>
        <i class="only-expanded fa fa-remove"></i> III. Love</span></h2>
<div class="divArticle collapse in article" id="b3">

In other word, DOM has given it the necessary function, correctly numbering each id sequentially. All that's missing is the styling:

<span class="Article"><span class="label label-primary"><i class="only- collapsed fa fa-chevron-down"></i><i class="only-expanded fa fa-remove"> </i> III. Love</span></span>

Can anyone tell me how to add that styling? The titles will change, of course (e.g. III. Love, IV. Hate, etc.). I posted my DOM script below:

$i = 1; // initialize counter
$dom = new DOMDocument;
@$dom->loadHTML($Content); // load the markup
$sections = $dom->getElementsByTagName('section'); // get all section tags
    foreach($sections as $section) { // for each section tag

        // get div inside each section
        foreach($section->getElementsByTagName('h2') as $h2) {
            if($h2->getAttribute('class') == 'h2Article') { // if this div has class maindiv
                $h2->setAttribute('id', 'a' . $i); // set id for div tag
                $h2->setAttribute('data-target', '#b' . $i);
            }
        }

        foreach($section->getElementsByTagName('div') as $div) {
            if($div->getAttribute('class') == 'divArticle') { // if this div has class divArticle
                $div->setAttribute('id', 'b' . $i); // set id for div tag
            }

            if($div->getAttribute('class') == 'divClose') { // if this div has class maindiv
                $div->setAttribute('data-target', '#b' . $i); // set id for div tag
            }
        }
        $i++; // increment counter
    }

// back to string again, get all contents inside body
$Content = '';
foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $child) {
    $Content .= $dom->saveHTML($child); // convert to string and append to the container
}

$Content = str_replace('data-target', 'data-toggle="collapse" data-target', $Content);
$Content = str_replace('<div class="divArticle', '<div class="divArticle collapse in article', $Content);
  • 写回答

2条回答 默认 最新

  • douguo7431 2016-02-27 00:53
    关注

    Well, I guess the obvious solution is to just wrap the title in something that can be modified with a simple str_replace...

    <h2><span class="Answer">IIII. Love</span></h2>
    

    Or even this...

    <h2>[]III. Love[]</h2>
    

    Kind of Mickey Mouse, but it gets the job done. I just having to write out or paste all of that code into every heading in every article. I prefer to automate it as much as possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题