php中的不同摘录

php中的不同摘录,php,wordpress,Php,Wordpress,您好,如何使用php摘录此代码 这是wordpress的内容。 但是wordpress中没有这种方式 来自 <div class='first'>first</div> <div class='second'>second</div> <div class='first'>first</div> <div class='second'>second</div> <div class='first

您好,如何使用php摘录此代码

这是wordpress的内容。 但是wordpress中没有这种方式

来自

<div class='first'>first</div>
<div class='second'>second</div>
<div class='first'>first</div>
<div class='second'>second</div>
<div class='first'>first</div>
<div class='second'>second</div>
.
.
.
首先
第二
第一
第二
第一
第二
.
.
.

<div class='first'>first</div>
<div class='second'>second</div>
首先
第二
您可以使用以下方法进行尝试:

//如果在$html这样的变量中检索html,则
$html=“第一
第二
第一
第二
第一
第二”;
$validate=array('first'=>true,'second'=>true);
$response=trim(preg_replace_回调('/'),函数($match)使用(&$validate){
重置($validate);$key=key($validate);
if(strstrstr($match[0],$key)!=false&&$validate[$key]==true){
$validate[$key]=false;
返回$match[0];
}
next($validate);$key=key($validate);
if(strstrstr($match[0],$key)!=false&&$validate[$key]==true){
$validate[$key]=false;
返回$match[0];
}
},$html));
var_dump($response);

您可以使用以下css将div隐藏在第二个div之后:

div.first, div.second {
    display: none;
}
div.first:first-child,
div.second:first-child {
    display: block;
}
要在PHP中完全删除标记中的div,应该需要正则表达式,但是


如果你真的需要那样做,你最好考虑把你的div的内容存储在不同的领域。

你问的不是很清楚。这个div是如何生成的?他们来自哪里?正如rnevius所指出的,您的HTML是无效的-您缺少所有
标记的结尾
。哦,不!这是我在stackoverflow中输入的错误!html在一个变量中?是的,它是wordpress metaboxI中的一个字段。我认为这个正则表达式不应该工作,如果你想匹配所有div的内容,你可以使用
/(.*)/
。请参阅@vard是否执行了php?@vard preg_replace_回调从模式中获取一个匹配循环。刚刚执行,它确实有效。糟糕的是,我被正则表达式似乎与类名+内容匹配的事实弄糊涂了。
div.first, div.second {
    display: none;
}
div.first:first-child,
div.second:first-child {
    display: block;
}