我正在尝试自学PHP ...所以请善待并忍受我。

我正在尝试关注本教程关于如何缓存文件...我想要缓存的页面只是HTML,所以我修改了php来处理数据。我知道缓存部分正在工作,当我尝试修改结果时,我得到一个“可捕获的致命错误:类缓存的对象无法转换为字符串”在下面的str_replace行中。

我尝试过使用 __ toString方法,我尝试过使用序列化。有什么我想念的吗?

编辑:哦,我甚至尝试过投射操作员

 $caching = new Caching( "my.htm", "http://www.page-I-want.com/" );
 $info = new TestClass($caching);
 $info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );

我的var_dump($ caching);如下:

object(Caching)#1 (2) { ["filePath"]=>  string(9) "cache.htm" ["apiURI"]=>  string(27) "http://www.page-I-want.com/" } 

好的,我现在看到问题在于caching.php没有将值返回到$ caching字符串。任何人都可以看看下面的链接,并帮我弄清楚它为什么不起作用?谢谢!

我刚刚发布了我的整个caching.php文件此处

有帮助吗?

解决方案

您链接的网站上的代码通过从您提供的URL下载页面并为艺术家解析它,然后将其保存到缓存文件中。缓存对象只包含两个变量; filePath和apiURI。如果要修改页面的解析方式并将其转换为缓存的XML文件,则应更改stripAndSaveFile函数。

以下是如何修改Caching.php以执行所需操作的示例:

  function stripAndSaveFile($html) {
        //mange the html code in any way you want
        $modified_html = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $html );
        //save the xml in the cache
        file_put_contents($this->filePath, $modified_html);  
  }         

编辑:

其他选项是使用您可以执行的类在您的php代码中扩展Caching类:

  class SpecialCaching extends Caching {
        var $html = "";
        function stripAndSaveFile($html) {
              //mange the html code in any way you want
              $this->html = $html;
        }
  }

  $caching = new SpecialCaching( "my.htm", "http://www.page-I-want.com/" );
  $info = $caching->html;
  $info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top