2009-09-16 125 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