php判断文件里面的类名

怎么判断文件名是**_shop.html的时候里面的类名.info消失
比如:
news_list.html
product_list.html
news_shop.html
product_shop.html

只要是_shop.html的时候就类名.info消失

html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>判断变化title>
    <style>
        .info {
            width: 200px;
            height: 200px;
            background-color: red;
        }
    style>
head>

<body>
   
    <div class="info">div>

body>
html>





参考下面两个案例:

<?php
$file_name = 'product_shop.html';

// 判断文件名是否以 '_shop.html' 结尾
if (substr($file_name, -10) === '_shop.html') {
  // 如果是,则删除类名中的 '.info'
  echo '<div class="info"></div>';
} else {
  // 如果不是,则正常输出类名
  echo '<div class="info.info"></div>';
}
?>
<?php
$file = 'news_shop.html'; // 这里是文件名
$filename = pathinfo($file, PATHINFO_FILENAME); // 获取文件名(不含扩展名)
if (strpos($filename, '_shop') !== false) { // 如果文件名中包含 '_shop'
    $html = file_get_contents($file); // 读取文件内容
    $html = str_replace('info', '', $html); // 替换掉 'info'
    file_put_contents($file, $html); // 保存修改后的内容
}
?>

可以通过正则表达式匹配文件名,找到所有以"_shop.html"结尾的文件名,然后针对这些文件名中的类名进行处理。

下面是一种示例的JavaScript代码,可以匹配以"_shop.html"结尾的文件名,并使用正则表达式去除其内部class为"info"的元素的类名。其中,假设所有文件名都存储在一个名为filenames的数组中。

for (let i = 0; i < filenames.length; i++) {
  if (filenames[i].match(/_shop\.html$/)) {
    let filename = filenames[i];
    console.log('处理文件:', filename);
    let fileContent = fs.readFileSync(filename, 'utf8');
    fileContent = fileContent.replace(/<[^<>]+class="?info"?[^<>]*>/gi, function(match) {
      return match.replace(/\s*class="?info"?/gi, '');
    });
    fs.writeFileSync(filename, fileContent);
  }
}

上述代码使用了Node.js中的fs模块来读取和写入文件。在运行时,需安装和引用该模块,同时保证代码所在的环境可以访问到需要处理的文件。