如何从许多php文件中删除某些部分代码。

时间:2016-06-16 02:26:25

标签: php html list

我是php的新手,我创建了很多文件,现在我需要从许多php文件中删除某些部分代码。我知道这个问题听起来很熟悉,但我需要一个非常基本和简单的解释,就像初学者一样。

<li style="opacity: 1;" class="span3">
    <div class="thumbnail"><a href="cd_sticker_printing_sticker.php" class="thumb"><img src="brochure/cdstickercoverprinting/sticker/1 (3).jpg" alt="Product"></a>
      <p><a href="cd_sticker_printing_sticker.php">STICKER</a></p>
      <p class="price"><span>€ 1.230</span></p>
      <p class="rating"><i class="icon-star"><span>1</span></i><i class="icon-star"><span>2</span></i><i class="icon-star"><span>3</span></i><i class="icon-star"><span>4</span></i><i class="icon-star-empty"><span>5</span></i></p>
      <a href="enquiry.php"><input value="For Enquiry" class="btn" type="button"></a>
      <a href="#" class="add-list"><i class="icon-star"></i>Wish List</a><a href="#" class="add-comp"><i class="icon-tasks"></i>Compare</a></div>
  </li>

我需要从上面的列表中删除<p class="rating"><i class="icon-star"><span>1</span></i><i class="icon-star"><span>2</span></i><i class="icon-star"><span>3</span></i><i class="icon-star"><span>4</span></i><i class="icon-star-empty"><span>5</span></i></p><a href="#" class="add-list"><i class="icon-star"></i>Wish List</a><a href="#" class="add-comp"><i class="icon-tasks"></i>Compare</a>

1 个答案:

答案 0 :(得分:0)

请在运行之前进行备份,但php答案是:

$directory_with_files = "your\\directory"; 
$string_to_replace = '<li style="opacity: 1;" class="span3">
    <div class="thumbnail"><a href="cd_sticker_printing_sticker.php" class="thumb"><img src="brochure/cdstickercoverprinting/sticker/1 (3).jpg" alt="Product"></a>
      <p><a href="cd_sticker_printing_sticker.php">STICKER</a></p>
      <p class="price"><span>€ 1.230</span></p>
      <p class="rating"><i class="icon-star"><span>1</span></i><i class="icon-star"><span>2</span></i><i class="icon-star"><span>3</span></i><i class="icon-star"><span>4</span></i><i class="icon-star-empty"><span>5</span></i></p>
      <a href="enquiry.php"><input value="For Enquiry" class="btn" type="button"></a>
      <a href="#" class="add-list"><i class="icon-star"></i>Wish List</a><a href="#" class="add-comp"><i class="icon-tasks"></i>Compare</a></div>
  </li>';

//Get the files in the directory
$files = scandir($directory_with_files);

//Get rid of ".." and "." as files.
unset($files[0]);
unset($files[1]);

//Loop through them
foreach($files as $file){

   //read the entire string
   $str=file_get_contents($directory_with_files."\\".$file);

   //replace something in the file string - this is a VERY simple example
   $str=str_replace("$string_to_replace","",str);

   //write the entire string
   file_put_contents($directory_with_files."\\".$file, $str);
}//end foreach loop

我没有运行这个,所以你可能不得不在这里和那里调试一些东西。

此外,将来,您可能会发现一次使用include(“yourfile.ext”)更容易,而不必编辑每个页面。当您前进一点时,您可能会使用视图来调用像这样的页面标记。