2009-09-16 110 views
0

我想教自己的PHP ...所以请善待和忍受我。php对象字符串

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

我试过使用__toString method here,我试过使用serialize。有什么我失踪?

编辑:哦,我甚至试过casting operators

$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.php文件here

+0

你想在对象序列化的时候替换图像,然后反序列化它吗? – null 2009-09-16 16:45:00

+0

所以你在Caching类中实现了__toString方法?它是什么样子的? (首先调用序列化时不需要,但最好不要更改序列化对象的内容) – Joost 2009-09-16 16:48:36

+0

如果可以添加var_dump($ caching);打印。 – Raynet 2009-09-16 17:05:05

回答

1

您链接的网站上的代码通过从您提供的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代码你可以做的类:

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); 
+0

我重命名stripAndSaveFile函数,并且我不想修改caching.php文件中的数据,因为我将其用于其他文件和替换不同的对象 - 我将功能添加到顶端的帖子。 – Mottie 2009-09-16 18:19:51

+0

我放弃了并替换了缓存中的字符串。PHP文件,现在它的作品...感谢您的帮助! – Mottie 2009-09-17 04:13:35