简体   繁体   English

PHP或Regex获得唯一的匹配ID

[英]PHP or Regex for unique matching ID's

Here's some stuff stored in a database table: 这是存储在数据库表中的一些东西:

<section id="earth">
  <h2>Earth</h2>
  <div class="MainDiv">
    (article)
  </div>
</section>

<section id="mars">
  <h2>Mars</h2>
  <div class="MainDiv">
    (article)
  </div>
</section>

All I want to do is modify the display so each section has a unique data-target and each div has a matching ID. 我要做的就是修改显示,以便每个部分都有一个唯一的数据目标,每个div都有一个匹配的ID。 (I gave the div's a class - MainDiv - to distinguish them from any nested divs they might contain.) (我给div的一个类-MainDiv-区分它们与它们可能包含的任何嵌套div。)

<section id="earth" data-target="a">
  <h2>Earth</h2>
  <div class="MainDiv" id="a">

<section id="mars" data-target="b">
  <h2>Mars</h2>
  <div class="MainDiv" id="b">

Ideally, I'd like to keep the data-targets and matching ID's simple - single, lower case letters would be perfect. 理想情况下,我想保持数据目标和匹配ID的简单性-单个小写字母将是完美的选择。 Is there a PHP function that can do that? 有没有可以做到这一点的PHP函数? If not, do you know a regex script that would work? 如果不是,您是否知道可以使用的正则表达式脚本?

No, as much as possible, don't work on regex for this task. 不,尽可能不要在正则表达式上执行此任务。 This is what HTML parsers are for. 这就是HTML解析器的用途。 There is already a class for PHP that does this, its DOMDocument . 已经有用于PHP的类,即DOMDocument

First, just load up the class (this is built-in already), then you load the HTML markup, get all the elements you need (in this case, the section tags). 首先,只需加载类(该类已内置),然后加载HTML标记,获取所需的所有元素(在本例中为section标签)。 After you got all the section tags, iterate on those found and add those data attributes you want for each of those (including the div tags), then you put it back together as string again. 获得所有section标记后,对找到的那些标记进行迭代,并为每个标记添加所需的数据属性(包括div标记),然后再次将其重新组合为字符串。 Here's some to get you started. 以下是一些入门指南。 Rough Example: 粗略的例子:

$i = 1; // initialize counter
// initialize DOMDocument
$dom = new DOMDocument;
@$dom->loadHTML($html); // load the markup

$sections = $dom->getElementsByTagName('section'); // get all section tags
if($sections->length > 0) { // if there are indeed section tags inside
    // work on each section
    foreach($sections as $section) { // for each section tag

        $section->setAttribute('data-target', 'section' . $i); // set id for section tag

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

        $i++; // increment counter
    }
}

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

// output
echo $html;

Sample Output 样本输出

I would do as @Barmar suggested. 我会像@Barmar建议的那样做。 Doing a0 , a1 , a2 , ..., This way we can have as many as we want and echo each <section> in PHP via loop. 这样做a0a1a2 ,...,这样我们就可以有很多,因为我们希望和echo<section>通过循环在PHP。 If we went from a , b , c ,..., z what would be next? 如果我们从abc ,..., z走下去,下一步将是什么? aa ? aa And you will have to program that in. For each element I would use a simple for loop and use the index for each element. 您将必须对其进行编程。对于每个元素,我都将使用简单的for循环,并对每个元素使用索引。 For example this: 例如:

class Planet
{
    public $title;
    public $name;
    public $article;
    function __construct($title, $name, $article) {
        $this->name = $name;
        $this->title = $title;
        $this->article = $article;
    }
}

$planets = array(
    new Planet("earth", "Earth", "Hello World!"),
    new Planet("mars", "Mars", "Hello World Again!")
);

for( $i = 0; $i < count($planets); $i++ ) {
    $id = strtolower($planets[$i]->name);
    $name = $planets[$i]->name;
    $article = $planets[$i]->article;
    echo <<<HERE
<section id="$id" data-target="a$i">
  <h2>$name</h2>
  <div class="MainDiv" id="a$i">
    $article
  </div>
</section> 

HERE;
}

Would give the output: 将给出输出:

<section id="earth" data-target="a0">
  <h2>Earth</h2>
  <div class="MainDiv" id="a0">
    Hello World!
  </div>
</section>  
<section id="mars" data-target="a1">
  <h2>Mars</h2>
  <div class="MainDiv" id="a1">
    Hello World Again!
  </div>
</section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM