2010-05-22 41 views
1

我正在尋找一個正則表達式,在一個圖像標記(SRC)找到所有圖像路徑和CID變換所有圖像路徑:文件名正則表達式找到圖像路徑文件中的圖像標籤

<img src="../images/text.jpg" alt="test" /> 

<img src="cid:test" alt="test" /> 

感謝您的幫助

克里斯

+0

只是爲了澄清:src =「../ images/text.jpg」應該是src =「../ images/test.jpg」或者你真的想要將alt屬性的值插入爲cid嗎? – Max 2010-05-23 08:29:47

回答

0

你可以去PHP DOM解析你的html,並做相應的搜索替換。

否則,你也可以使用JQuery要做到這很容易做到的東西:

$(function(){ 
    $('img').each(function(){ 
    $(this).attr('src').replace('cid:' + $(this).attr('src')); 
    }); 
}); 
+0

感謝您的快速回答,但您對PHP正則表達式有任何想法嗎? – Chris 2010-05-22 21:32:36

+0

@Chris:你可以使用'preg_replace'函數,但我認爲那裏不需要regex。 – 2010-05-22 21:36:06

+0

嗯,我需要一個正則表達式,因爲它將成爲一個用於通訊的模板 – Chris 2010-05-22 21:39:34

2

隨着Web邏輯的建議,我寧願給PHP DOM擴展一試,特別是如果你的工作與一個完整的HTML文件。您可以將某個HTML片段傳遞給PHP DOM的實例或完整的HTML頁面的內容。如何你有什麼建議,如果你只是有一個像<img src="../images/text.jpg" alt="test" />圖像元素的字符串,並要設置它的src屬性添加到圖像文件名沒有通過cid:

<?php 
$doc = new DOMDocument(); 
// Load one or more img elements or a whole html document from string 
$doc->loadHTML('<img src="../images/text.jpg" alt="test" />'); 

// Find all images in the loaded document 
$imageElements = $doc->getElementsByTagName('img'); 
// Temp array for storing the html of the images after its src attribute changed 
$imageElementsWithReplacedSrc = array(); 

// Iterate over the found elements 
foreach($imageElements as $imageElement) { 
    // Temp var, storing the value of the src attribute 
    $imageSrc = $imageElement->getAttribute('src'); 
    // Temp var, storing the filename with extension 
    $filename = basename($imageSrc); 
    // Temp var, storing the filename WITHOUT extension 
    $filenameWithoutExtension = substr($filename, 0, strrpos($filename, '.')); 
    // Set the new value of the src attribute 
    $imageElement->setAttribute('src', 'cid:' . $filenameWithoutExtension); 

    // Save the html of the image element in an array 
    $imageElementsWithReplacedSrc[] = $doc->saveXML($imageElement); 
} 

// Dump the contents of the array 
print_r($imageElementsWithReplacedSrc); 
爲前綴的文件擴展名

一個例子

打印這個結果(在Windows Vista上使用PHP 5.2.x):

Array 
(
    [0] => <img src="cid:text" alt="test"/> 
) 

如果你想設置的值在src屬性由前綴cid: alt屬性的值,看看這個:

<?php 
$doc = new DOMDocument(); 
// Load one or more img elements or a whole html document from string 
$doc->loadHTML('<img src="../images/text.jpg" alt="test" />'); 

// Find all images in the loaded document 
$imageElements = $doc->getElementsByTagName('img'); 
// Temp array for storing the html of the images after its src attribute changed 
$imageElementsWithReplacedSrc = array(); 

// Iterate over the found elements 
foreach($imageElements as $imageElement) { 
    // Set the new value of the src attribute 
    $imageElement->setAttribute('src', 'cid:' . $imageElement->getAttribute('alt')); 

    // Save the html of the image element in an array 
    $imageElementsWithReplacedSrc[] = $doc->saveXML($imageElement); 
} 

// Dump the contents of the array 
print_r($imageElementsWithReplacedSrc); 

打印:

Array 
(
    [0] => <img src="cid:test" alt="test"/> 
) 

我希望得到你開始。這些只是如何處理DOM擴展的例子,您需要解析什麼的描述(HTML片段或完整的HTML文檔)以及您需要輸出/存儲的內容有點模糊。

相關問題