1

我正在寻找 php 中特定元素的标签,我想我可以通过 preg_match 做到这一点,但我不确定。

基本上我有类似的东西:

<label>Some Jazz</label>

我想做的是:

if tag = label 然后做一些事情,否则做其他事情。

问题是从 html 标签中获取标签部分。我没有找到这个答案。欢迎您的想法。

我没有提到我不想要“一些爵士乐”,我想看看标签是否是标签。- 这是参考一些答案,它让我知道标签之间的内容,而不是标签本身。

4

2 回答 2

0

我喜欢使用 Simple HTML DOM 进行解析

http://simplehtmldom.sourceforge.net/
于 2012-12-04T03:51:21.890 回答
0

看看是不是:

<?php

//External html or you can set this by string
$html = file_get_contents("http://example.com/"); 

//Parse html to object
$doc = new DOMDocument();
@$doc->loadHTML($html); //To prevent some html errors

//Labels
$labels = $doc->getElementsByTagName('label');

//For each label
for ($i = 0; $i < $labels->length; $i++) {

    $label = $labels->item($i);

    //Check label value
    if($label->nodeValue == 'something') {
        //Do something
    }
    else {
        //Do something
    }
}

?>
于 2012-12-04T04:03:03.833 回答