2

I would like to know if there is any way, in php, to match all classes with the same word, Example:

<div class="classeby_class">
  <div class="classos-nope">
    <div class="row">
      <div class="class-show"></div> 
    </div>
   </div>
</div>

<div class="class-first-one">
  <div class="container">
    <div class="classes-show">
      <div class="class"></div>
      <div class="classing"></div> 
    </div>
   </div>
</div>

in the example above I would like to match all div that contain the word "class" but do not match those that have the word "classes" like,

positive for

<div class="class-show">...</div>
<div class="class-first-one">...</div>
<div class="class">...</div>
<div class="class-first-one">...</div>

but negative for

<div class="classeby_class">...</div> 
<div class="classes-show">...</div> 
<div class="classing">...</div> 

I am using php to display several different html pages. As regex would not be the appropriate method, first because of several page breaks, second because of hosting limitations, I'm trying to do this by parse.

All html code is stored on the server. I can liminate with a specific class using the example below.

$doc = new DomDocument();
$xpath = new DOMXPath($doc);
$classtoremove = $xpath->query('//div[contains(@class,"class")]');
foreach($classtoremove as $classremoved){
         $classremoved->parentNode->removeChild($classremoved);
}
echo $HTMLDoc->saveHTML();

I know there are CSS selectors, but when I try to use it in PHP it doesn't work. Possibly because I'm using XPath. Example:

'[id*="class"],[class*="class"]'

Still, I think he would take values beyond what I need. Any way to get these values by Xpath? the intent is to completely remove the div or other tags that contain that word.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Dg2A
  • 33
  • 4
  • Take a look at https://stackoverflow.com/questions/33441697/call-to-undefined-method-domdocumentgetelementsbyclassname/33446305#33446305 – ThW Dec 02 '21 at 09:32

1 Answers1

0

You could make use of a regex with word boundaries \bclass\b for the class attribtute and make use of DOMXPath::registerPhpFunctions.

For example

$data = <<<DATA
<div class="classeby_class">
  <div class="classos-nope">
    <div class="row">
      <div class="class-show"></div> 
    </div>
   </div>
</div>

<div class="class-first-one">
  <div class="container">
    <div class="classes-show">
      <div class="class"></div>
      <div class="classing"></div> 
    </div>
   </div>
</div>
DATA;


$doc = new DomDocument();
$doc->loadHTML($data);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions();
$classtoremove = $xpath->query("//div[1 = php:function('preg_match', '/\bclass\b/', string(@class))]");
foreach ($classtoremove as $a) {
    var_dump($a->getAttribute("class"));
}

Output

string(10) "class-show"
string(15) "class-first-one"
string(5) "class"

See a PHP demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70