【问题标题】:How to remove, certain part of the code from many php files.如何从许多 php 文件中删除某些部分代码。
【发布时间】:2016-06-16 02:26:25
【问题描述】:

我是 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>

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

【问题讨论】:

  • 你有 sublime 编辑器吗?可以这样做。
  • 你用什么程序来编辑你的代码?大多数将有一个查找和替换工具。您必须通过将代码复制到“查找”部分来搜索要删除的代码(一次一个),然后运行“全部替换”命令。如果您不确定如何调出编辑器的“查找和替换”工具,您需要 Google“ 在文件中查找和替换”
  • 为了将来参考,您可以使用 include("yourfile.ext") 并将该文件的内容转储到您拨打电话的地方。然后,如果需要更改,您可以在一处进行全部编辑。

标签: php html list


【解决方案1】:

请在运行之前进行备份,但 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) 一次会更容易,而不必编辑每个页面。当您进一步前进时,您可能会使用视图来调用在这样的页面标记上。

【讨论】:

    最近更新 更多