bir görüntü etiketi görüntü yolu dosyayı bulmak için sıradanifade

2 Cevap php

Ben bir resim etiketi (src) in all images path bulmak ve all images path cid tarafından dönüşümü regexp arıyorum: dosyaadı

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

karşı

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

Yardımlarınız için teşekkürler

Chris

2 Cevap

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.

Eğer html ayrıştırmak için PHP DOM için gidebilirsiniz ve arama buna göre değiştiririm.

Aksi takdirde, aynı zamanda kolayca şeyler yapmak bunun için JQuery kullanabilirsiniz:

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