如何使用PHP获取从html到数组的所有类

如何使用PHP获取从html到数组的所有类,php,regex,Php,Regex,有些事情看起来很简单,但这件事从昨天起就一直折磨着我 我有一个css文件style.css,到目前为止,我已经能够解析数组中的所有内容。那里一切都好 我似乎不知道如何从html文件中获取所有类,以便将其与css数组进行比较 示例:我的代码如下所示 <div id="idtest" class="flex-center position-ref full-height"> <div class="flex-test1">

有些事情看起来很简单,但这件事从昨天起就一直折磨着我

我有一个css文件style.css,到目前为止,我已经能够解析数组中的所有内容。那里一切都好

我似乎不知道如何从html文件中获取所有类,以便将其与css数组进行比较

示例:我的代码如下所示

      <div id="idtest" class="flex-center position-ref full-height">
            <div class="flex-test1">
                 <div class="flex-test2">
                 </div>
            </div>
      </div>


我想您可以尝试使用jQuery.each()[此处]
它将迭代DOMs元素,您可以获得该类。。(我没有50%的声誉将此作为评论…)

我认为您可以尝试使用jQuery.each()[此处]
它将迭代DOMs元素,您可以获得该类。。(我没有50%的声誉将此作为评论,所以…)

您设计的表达很好,我们可能需要添加一个捕获组,问题可能已经解决:

$re = '/class="\s*(.*?)\s*"/s';
$str = '      <div id="idtest" class="flex-center position-ref full-height">
            <div class="flex-test1">
                 <div class="flex-test2">
                 </div>
            </div>
      </div>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);
$re='/class=“\s*(.*?\s*”/s';
$str='1
';
预匹配全部($re,$str,$matches,预设置顺序,0);
var_dump($matches);

在此,如果您可能感兴趣,将对表达式进行解释。

您设计的表达式很好,我们可能希望添加一个捕获组,问题可能已解决:

$re = '/class="\s*(.*?)\s*"/s';
$str = '      <div id="idtest" class="flex-center position-ref full-height">
            <div class="flex-test1">
                 <div class="flex-test2">
                 </div>
            </div>
      </div>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);
$re='/class=“\s*(.*?\s*”/s';
$str='1
';
预匹配全部($re,$str,$matches,预设置顺序,0);
var_dump($matches);

在这里,如果您可能感兴趣,将解释表达式。

Your want
preg\u match\u all
另一种方法,最好使用xpath,然后在以后不做太多更改的情况下,或者regex只能从div中选择,等等,您真是太棒了。谢谢你想要的
preg\u match\u all
另一种方法,最好是使用xpath,以后不用做太多更改,或者正则表达式只能从div中选择,等等,你太棒了。谢谢谢谢,很遗憾我一直在寻找用php来完成这项工作。谢谢,很遗憾我一直在寻找用php来完成这项工作。太棒了,谢谢。我添加了id=“\s*(*?)\s*”| class=“\s*(*?)\s*”并根据需要提取了所有内容。非常感谢。我添加了id=“\s*(*?)\s*”| class=“\s*(*?)\s*”并根据需要提取所有内容。
$re = '/class="\s*(.*?)\s*"/s';
$str = '      <div id="idtest" class="flex-center position-ref full-height">
            <div class="flex-test1">
                 <div class="flex-test2">
                 </div>
            </div>
      </div>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);