Web Logic olarak önerilen, ben değil bütün bir HTML belgesi ile çalışan, özellikle PHP DOM Extension bir denemek istiyorum. Ya PHP DOM ya da tam bir HTML sayfasının içeriğinin bir örneğine bazı HTML parçasını iletebilirsiniz.
Sadece <img src="../images/text.jpg" alt="test" /> gibi bir görüntü elemanın bir dize var ve dosyası olmadan görüntü dosya için bunun src özniteliğini ayarlamak istiyorsanız önermek ne yapmak nasıl bir örnek cid: ile başlayan uzantısı
<?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 üzerinde PHP 5.2.x kullanarak) bu sonucu yazdırır:
Array
(
[0] => <img src="cid:text" alt="test"/>
)
Eğer tarafından öneki alt niteliğinin değeri src öznitelik değerini ayarlamak istiyorsanız cid:, bu bak:
<?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);
Baskılar:
Array
(
[0] => <img src="cid:test" alt="test"/>
)
Ben başlamak alır umuyoruz. Bu DOM uzantısı, (HTML parçaları veya tam HTML belgesi) ayrıştırmak için gereken ne tanımını ve ne çıktı / saklamak için gerek biraz muğlak ile ne yapacağını sadece örnektir.