查找带有类的元素并在php字符串中再添加一个
我正试图在wordpress中制作自己的音频短代码过滤器。我想找到一个带有类的元素,然后再添加一个。我有一个带有html代码的字符串,str_replace不起作用查找带有类的元素并在php字符串中再添加一个,php,wordpress,class,audio,add,Php,Wordpress,Class,Audio,Add,我正试图在wordpress中制作自己的音频短代码过滤器。我想找到一个带有类的元素,然后再添加一个。我有一个带有html代码的字符串,str_replace不起作用 <div id="mep_0" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;"> 我想用“mejs
<div id="mep_0" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">
我想用“mejs container”将“测试类”添加到div中。我想补充的是,id('mep_0')不是固定的。您可以使用它
这是一个简单的例子:
$dom = '<div id="mep_0" class="mejs-container svg mejs-audio" tabindex="1" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">
<div id="mep_1" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">
<div id="mep_2" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">';
$xpath = new DomXPath($dom); // read xml/html into dom to manipulate or search stuff
$elements = $xpath->query('//div[contains(@class,"mejs-container")]'); // search elements that contain "mejs-container" class
// loop through each found element
foreach($elements as $element) {
// get the current class attributes content
// which would give you "mejs-container svg mejs-audio"
$currentClass = $element->getAttribute('class');
// append "test-class" to the current value and set it in the dom (mind the space between classes)
$element->setAttribute($currentClass . " test-class");
}
$dom=
';
$xpath=new-DomXPath($dom);//将xml/html读入dom以操作或搜索内容
$elements=$xpath->query('//div[contains(@class,“mejs容器”));//搜索包含“mejs容器”类的元素
//循环遍历找到的每个元素
foreach($elements作为$element){
//获取当前类属性内容
//这将为您提供“mejs容器svg mejs音频”
$currentClass=$element->getAttribute('class');
//将“test class”附加到当前值并在dom中设置它(注意类之间的间距)
$element->setAttribute($currentClass.“测试类”);
}
您可以为此目的使用
这是一个简单的例子:
$dom = '<div id="mep_0" class="mejs-container svg mejs-audio" tabindex="1" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">
<div id="mep_1" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">
<div id="mep_2" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="Audio Player" style="width: 416px; height: 30px;">';
$xpath = new DomXPath($dom); // read xml/html into dom to manipulate or search stuff
$elements = $xpath->query('//div[contains(@class,"mejs-container")]'); // search elements that contain "mejs-container" class
// loop through each found element
foreach($elements as $element) {
// get the current class attributes content
// which would give you "mejs-container svg mejs-audio"
$currentClass = $element->getAttribute('class');
// append "test-class" to the current value and set it in the dom (mind the space between classes)
$element->setAttribute($currentClass . " test-class");
}
$dom=
';
$xpath=new-DomXPath($dom);//将xml/html读入dom以操作或搜索内容
$elements=$xpath->query('//div[contains(@class,“mejs容器”));//搜索包含“mejs容器”类的元素
//循环遍历找到的每个元素
foreach($elements作为$element){
//获取当前类属性内容
//这将为您提供“mejs容器svg mejs音频”
$currentClass=$element->getAttribute('class');
//将“test class”附加到当前值并在dom中设置它(注意类之间的间距)
$element->setAttribute($currentClass.“测试类”);
}
你想在每个mejs容器
元素中添加测试类
吗?是的,这就是我想要的为什么不替换工作?我不知道…你想在每个mejs容器
元素中添加测试类
吗?是的,这就是我想要的为什么不替换工作?我不知道…我仍然不知道怎么做:(我在每行添加了一条注释来解释发生了什么以及$dom是如何设置的。您还可以让它使用$file=“test.HTML”$dom=new-DOMDocument();$dom->loadHTMLFile($file)从文件加载HTML);
而不是上面的第一行。如果你不知道如何处理wordpress中的php内容,你也可以使用javascript来处理你的标记。我仍然不知道如何做:(我在每行添加了一条注释来解释发生了什么以及$dom是如何设置的。你也可以让它使用$file=“test.HTML”从文件加载HTML);$dom=new DOMDocument();$dom->loadHTMLFile($file);
而不是上面的第一行。如果你不知道如何处理wordpress中的php内容,你也可以使用javascript来处理你的标记。