正则表达式 - 查找具有某些ID的所有元素

时间:2022-07-26 19:29:14

I am trying to find all <*style> tags within HTML, with only a certain ID for instance (code should only return the tags with fixed_id):

我试图在HTML中找到所有<* style>标签,例如只有一个ID(代码应该只返回带有fixed_id的标签):

<style id="fixed_id" type="text/css">all css code</style>

I have the following code..

我有以下代码..

$html = '<style id="fixed_id" type="text/css">css_code</style><style id="ignore_this" type="text/css"></style>';    
preg_match_all("@<style[^>]+(id=['\"fixed_id\"]?)[^>]+(type=['\"]?[^'\"]+['\"]?)?[^>]+?>(.+?)</style>@is", $html, $matches);

I also need to remove all those <*style> tags with certain IDs, for that I am using the following code which obviously is not selecting the right selector but removing all <*style> tags.

我还需要删除具有某些ID的所有<* style>标签,因为我使用以下代码,显然不是选择正确的选择器而是删除所有<* style>标签。

$html = preg_replace("/<style\\b[^>]*>(.*?)<\\/style>/s", "", $html);

1 个解决方案

#1


2  

Notice: Assigning same ID to multiple tags is semantically wrong. One tag - one ID.

注意:为多个标签分配相同的ID在语义上是错误的。一个标签 - 一个ID。

For removing all those <*style> tags with certain ID:

要删除具有特定ID的所有<* style>标记:

$html = '<style id="ignore_this" type="text/css"></style><style class="test_class" id="fixed_id" type="text/css">css_code</style><style id="ignore_this" type="text/css"></style>';

$id = "fixed_id"; // could be any other value
$res = preg_replace('/<style(\s+([a-z\-]+)=(\'|\")([^\"\'>]*)(\'|\"))* id="' . $id .'" .*?>.*?<\/style>/i', "", $html);

var_dump($res);
// outputs '<style id="ignore_this" type="text/css"></style><style id="ignore_this" type="text/css"></style>'

To find <*style> tag with only a certain ID:

要查找仅包含特定ID的<* style>标记:

// I've changed IDs to be unique
$html = '<style id="ignore_this" type="text/css"></style><style class="test_class" id="fixed_id" type="text/css">css_code</style><style id="ignore_this2" type="text/css"></style><style data-id="111" id="fixed_id2" type="text/css">css content</style>';
preg_match_all('/<style(\s+([a-z\-]+)=(\'|\")([^\"\'>]*)(\'|\"))* id="' . $id .'" .*?>(?P<value>.*?)<\/style>/i', $html, $matches);

var_dump($matches['value']);  // using named submask to indicate captured tag value(text)
// outputs value of the captured tag as expected
0 => string 'css_code' 

I also recommend you to use DOMDocument for such goals(manipulating html content). It lets you to process html content in more precise and extensive way.

我还建议你使用DOMDocument来实现这些目标(操纵html内容)。它允许您以更精确和更广泛的方式处理html内容。

$doc = new \DOMDocument();
$doc->loadHTML($html); // $html from above

$xpath = new \DOMXPath($doc);
$id = "fixed_id"; // could be any other value
foreach ($xpath->query('//style[@id="'.$id.'"]') as $node) {
    echo $node->nodeValue;
}
// outputs 'css_code' as expected  

#1


2  

Notice: Assigning same ID to multiple tags is semantically wrong. One tag - one ID.

注意:为多个标签分配相同的ID在语义上是错误的。一个标签 - 一个ID。

For removing all those <*style> tags with certain ID:

要删除具有特定ID的所有<* style>标记:

$html = '<style id="ignore_this" type="text/css"></style><style class="test_class" id="fixed_id" type="text/css">css_code</style><style id="ignore_this" type="text/css"></style>';

$id = "fixed_id"; // could be any other value
$res = preg_replace('/<style(\s+([a-z\-]+)=(\'|\")([^\"\'>]*)(\'|\"))* id="' . $id .'" .*?>.*?<\/style>/i', "", $html);

var_dump($res);
// outputs '<style id="ignore_this" type="text/css"></style><style id="ignore_this" type="text/css"></style>'

To find <*style> tag with only a certain ID:

要查找仅包含特定ID的<* style>标记:

// I've changed IDs to be unique
$html = '<style id="ignore_this" type="text/css"></style><style class="test_class" id="fixed_id" type="text/css">css_code</style><style id="ignore_this2" type="text/css"></style><style data-id="111" id="fixed_id2" type="text/css">css content</style>';
preg_match_all('/<style(\s+([a-z\-]+)=(\'|\")([^\"\'>]*)(\'|\"))* id="' . $id .'" .*?>(?P<value>.*?)<\/style>/i', $html, $matches);

var_dump($matches['value']);  // using named submask to indicate captured tag value(text)
// outputs value of the captured tag as expected
0 => string 'css_code' 

I also recommend you to use DOMDocument for such goals(manipulating html content). It lets you to process html content in more precise and extensive way.

我还建议你使用DOMDocument来实现这些目标(操纵html内容)。它允许您以更精确和更广泛的方式处理html内容。

$doc = new \DOMDocument();
$doc->loadHTML($html); // $html from above

$xpath = new \DOMXPath($doc);
$id = "fixed_id"; // could be any other value
foreach ($xpath->query('//style[@id="'.$id.'"]') as $node) {
    echo $node->nodeValue;
}
// outputs 'css_code' as expected