Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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 div类内容和数据属性?(预匹配全部)_Php_Html_Regex_Preg Match All - Fatal编程技术网

Php 正则表达式查找html div类内容和数据属性?(预匹配全部)

Php 正则表达式查找html div类内容和数据属性?(预匹配全部),php,html,regex,preg-match-all,Php,Html,Regex,Preg Match All,使用preg_match_,我只想在html中获得类和数据属性 下面的示例起作用,但它只返回类名称或仅返回数据id内容 我希望示例模式同时查找类和数据id内容 我应该使用哪个正则表达式模式 Html内容: <!-- I want to: $matches[1] == test_class | $matches[2] == null --> <div class="test_class"> <!-- I want to: $matches[1] == test_

使用preg_match_,我只想在html中获得类和数据属性

下面的示例起作用,它只返回名称或仅返回数据id内容

我希望示例模式同时查找类和数据id内容

我应该使用哪个正则表达式模式

Html内容:

<!-- I want to: $matches[1] == test_class  | $matches[2] == null -->
<div class="test_class"> 

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" data-id="1"> 

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div id="test_id" class="test_class" data-id="1">

<!-- I want to: $matches[1] == test_class test_class2 | $matches[2] == 1 -->
<div class="test_class test_class2" id="test_id" data-id="1">

<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div data-id="1" class="test_class test_class2" id="test_id" >

<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div id="test_id" data-id="1" class="test_class test_class2">

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" id="test_id" data-id="1">
$pattern = '/<(div|i)\s.*(class|data-id)="([^"]+)"[^>]*>/i';

preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);

无法正常工作的正则表达式:

<!-- I want to: $matches[1] == test_class  | $matches[2] == null -->
<div class="test_class"> 

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" data-id="1"> 

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div id="test_id" class="test_class" data-id="1">

<!-- I want to: $matches[1] == test_class test_class2 | $matches[2] == 1 -->
<div class="test_class test_class2" id="test_id" data-id="1">

<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div data-id="1" class="test_class test_class2" id="test_id" >

<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div id="test_id" data-id="1" class="test_class test_class2">

<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" id="test_id" data-id="1">
$pattern = '/<(div|i)\s.*(class|data-id)="([^"]+)"[^>]*>/i';

preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);

$pattern='/为什么不改用DOM解析器呢

您可以使用类似于
//div[@class或@data id]
的XPath表达式来定位元素,然后提取它们的属性值

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);
$divs = $xpath->query('//div[@class or @data-id]');
foreach ($divs as $div) {
  $matches = [$div->getAttribute('class'), $div->getAttribute('data-id')];
  print_r($matches);
}

演示~

为什么不改用DOM解析器呢

您可以使用类似于
//div[@class或@data id]
的XPath表达式来定位元素,然后提取它们的属性值

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);
$divs = $xpath->query('//div[@class or @data-id]');
foreach ($divs as $div) {
  $matches = [$div->getAttribute('class'), $div->getAttribute('data-id')];
  print_r($matches);
}

演示~

我支持Phil的回答,我认为HTML解析器是一条出路。它更安全,可以处理很多复杂的事情

话虽如此,如果您想在示例中尝试regex,它将是这样的:

<(?:div|i)(?:.*?(?:class|data-id)="([^"]+)")?(?:.*?(?:class|data-id)="([^"]+)")?[^>]*>
]*>

示例:

我支持Phil的回答,我认为HTML解析器是一条可行之路。它更安全,可以处理很多复杂的事情

话虽如此,如果您想在示例中尝试regex,它将是这样的:

<(?:div|i)(?:.*?(?:class|data-id)="([^"]+)")?(?:.*?(?:class|data-id)="([^"]+)")?[^>]*>
]*>

示例:

感谢您做了我数小时来一直在寻找的事情。是的,我认为使用DOM很方便。艾瓦拉兄弟,你好,伊布拉欣。我需要你给我的密码。但是有一个像这样的问题请帮帮我?您好@MertA.,您可以将
*?
替换为
[^ \非常感谢!!@Ibrahim我以为我会晚些回复,所以我问了一个新问题。请在问题url中写下答案:@MertA。欢迎:)关于
数据ss在div中不被接受,这是因为你没有添加
=
。感谢你做了我一直在寻找的事情。是的,我想使用DOM很方便。Eyvallah bro。再次你好@Ibrahim。我需要你给我的代码。但是有一个类似的问题,请帮助我?嗨@MertA.,你可以用
替换
*?
[^ \非常感谢!!@Ibrahim我以为我会晚些回复,所以我问了一个新问题。请在问题url中写下答案:@MertA。欢迎:)关于
数据ss在div中不被接受,这是因为你没有添加
=
。非常感谢!我也可以用这个方法分解CSS内容吗?@MertA。我不太清楚你的意思,但可能不是;你不能用DOM解析器解析CSS。对不起,我正在更正:我用这个方法更改html类名。我用另一个代码结构更改CSS文件中的类名。我在php中使用正则表达式,同时更改CSS文件中的类名。我想知道我是否可以同时做这两件事使用php的th DOM?@MertA。不,你不能用DOM解析器读取、解释或修改CSS。但是你可以更改HTML中的值。非常感谢!我也可以用这个方法分解CSS内容吗?@MertA。我不太清楚你的意思,但可能不是;你不能用DOM解析器解析CSS。我正在更正:我正在更改ht使用此方法的ml类名。我正在使用另一个代码结构更改css文件中的类名。我在php中使用正则表达式,同时更改css文件中的类名。我想知道是否可以使用php对DOM执行这两种操作?@MertA。不,您不能使用DOM解析器读取、解释或修改css。不过,您可以更改HTML中的值.