Php regexp在图像标记中查找图像路径文件

Php regexp在图像标记中查找图像路径文件,php,image,Php,Image,我正在寻找一个regexp,它可以在图像标记(src)中查找所有图像路径,并通过cid:filename转换所有图像路径 <img src="../images/text.jpg" alt="test" /> 到 谢谢你的帮助 Chris您可以使用解析您的html并相应地进行搜索替换 否则,您也可以使用来轻松完成以下任务: $(function(){ $('img').each(function(){ $(this).attr('src').replace('ci

我正在寻找一个regexp,它可以在图像标记(src)中查找所有图像路径,并通过cid:filename转换所有图像路径

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


谢谢你的帮助


Chris

您可以使用解析您的html并相应地进行搜索替换

否则,您也可以使用来轻松完成以下任务:

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

正如Web逻辑所建议的,我宁愿尝试一下PHP DOM扩展,尤其是如果您使用的是整个HTML文档。您可以将一些HTML片段传递给PHPDOM实例,也可以传递完整HTML页面的内容

举一个例子,说明如果您只是有一个像
这样的图像元素字符串,并且希望将其
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);
印刷品:

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

我希望这能让你开始。这些只是DOM扩展的示例,您对需要解析的内容(HTML片段或完整的HTML文档)以及需要输出/存储的内容的描述有点模糊

谢谢你的快速回答,但是你对PHP regexp有什么想法吗?@Chris:你可以使用
preg_replace
函数,但我认为那里不需要regex。我需要一个regexp,因为它将是一个模板,用于新闻稿,只是为了澄清这一点:src=“../images/text.jpg”应该是src=“../images/test.jpg”或者确实要将alt属性的值作为cid插入?
Array
(
    [0] => <img src="cid:text" alt="test"/>
)
<?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"/>
)