2010-12-17 68 views
1

嗨,大家好,我正在尋求重寫圖像路徑的幫助。如何重寫圖像路徑

讓我解釋一下.....

比方說,我正在2個應用程序。 APP-A和APP-B。我的APP-A安裝了一個插件,以純HTML格式生成菜單輸出並保存爲HTML文件。

我現在想將這個HTML文件導入到我的APP = B中,並將它用作它的菜單。 HTML文件使用require_once('../../app-a/menu.html');

文件看起來是這樣的.....

<div class="menu"> 
<li class="item27 parent root" > 
    <a class="daddy item image subtext" href="/xyz/cms/index.php> 
    <span> 
     <img src="tmpl/abc_template/images/icons/icon.png" alt="icon1.png" /> 
     Link 1 <em>subline</em> 
    </span> 
    </a> 
    </li> 
    <li class="item27 parent root" > 
    <a class="daddy item image subtext" href="/xyz/cms/index2.php> 
     <span> 
     <img src="tmpl/abc_template/images/icons/icon.png" alt="icon2.png" /> 
     Link 2 <em>subline</em> 
     </span> 
    </a> 
    </li> 
    <li class="item27 parent root" > 
    <a class="daddy item image subtext" href="/xyz/cms/index3.php> 
    <span> 
     <img src="templ/abc_template/images/icons/icon.png" alt="icon3.png" /> 
     Link 3 <em>subline</em> 
    </span> 
    </a> 
    </li> 
</div> 

我導入此文件到我的APP-B快到了罰款,但我有成像路徑問題。這些路徑是相對於APP-A生成的,我想改變它們,以便我的APP-B從正確的位置獲取它們。

例如,在我導入APP-B文件,

我想我的APP-B讀取圖像路徑前加上../../使這些路徑:

<img src="tmpl/abc_template/images/icons/icon.png" alt="icon1.png" /> 
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon2.png" /> 
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon3.png" /> 

變得像這些路徑

<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon1.png" /> 
<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon2.png" /> 
<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon3.png" /> 

請幫助。我正在使用支持jQuery的模板。因此,即使是jQuery解決方案也可以。

+0

APP-A插件是一個純php腳本嗎?你不能更新? – ajreal 2010-12-17 19:46:37

回答

1

嗯...我一直喜歡jQuery的解決方案......

$(function() { 
    $('.menu img').each(function() { 
     img_path = $(this).attr('src'); 
     $(this).attr({'src': '../../' + img_path}); 
     }); 
    }); 

測試和工程。

+0

我建議在服務器端進行修復,儘管(爲了seo的目的)。搜索引擎不會選擇修改後的圖像路徑。 – 65Fbef05 2010-12-17 22:38:52

+0

非常感謝,這很酷。我正在使用此功能,因此將其標記爲已接受。 – 2010-12-19 23:02:04

1

可能最簡單的方法就是在服務器上創建一個符號鏈接。

LN -s TMP1 ../../tmp1

假設這是一個Linux/Apache服務器。或者你也可以在你的Apache配置中做別名定義,或者使用.htaccess做同樣的事情。否則,你必須開始黑客入侵HTML並重新創建它,我確信有幾個庫可以這樣做,但可能有他們自己的警告和安全問題。

1

如果你只是想要一個快速的解決方案,你將可能有更多的成功看菜單的HTML文件轉換成字符串:

$menuString = file_get_contents('../../app-a/menu.html');

然後你可以使用正則表達式的相對路徑添加到每個src標籤的開始。

preg_replace('%"tmpl/%is', '"../../tmpl/', $menuString);

這個正則表達式是不是很強勁,但它應該給你一個起點。

+0

嘿謝謝你的回答。這聽起來像我可以做的事情。在我執行preg_replace之後,還有更多的幫助........我如何將這些新內容放入我的APP-B代碼中?我的意思是相同的語法。請不要介意,我是編程新手,剛開始學習。 – 2010-12-17 19:50:04

+0

在你有require_once語句之前,現在你可以編寫'echo $ menuString;'並且將HTML菜單打印到網頁上。 – Andrew 2010-12-17 21:46:31

+0

非常感謝,真的有效 – 2010-12-19 23:00:37