regexp在图像标签中查找图像路径文件 [英] regexp to find image path file in an image tag

查看:74
本文介绍了regexp在图像标签中查找图像路径文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个正则表达式,可以在图像标签(src)中找到所有图像路径,并通过cid:filename

I'm looking for a regexp that find all images path in an image tag (src) and transform all images path by cid:filename

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

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

感谢您的帮助

克里斯

推荐答案

正如Web Logic所建议的,我宁愿尝试一下PHP DOM扩展,尤其是在处理整个HTML文档时.您可以将一些HTML片段传递给PHP DOM实例,也可以传递完整HTML页面的内容.

As Web Logic suggested, I would rather give the PHP DOM Extension a try, especially if you are working with a whole HTML document. You can either pass some HTML fragment to an instance of PHP DOM or the contents of a complete HTML page.

一个示例示例,说明如果您只有图像元素的字符串(如<img src="../images/text.jpg" alt="test" />)并且想要将其src属性设置为图像文件名而没有以

One example of how to do what you suggest if you just have a string of an image element like <img src="../images/text.jpg" alt="test" /> and want to set the src attribute of it to the image-filename without the file extension prefixed by 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):

Prints this result (using PHP 5.2.x on Windows Vista):

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


如果要将src属性的值设置为以cid:为前缀的alt属性的值,请查看以下内容:


If you want to set the value of the src attribute to the value of the alt attribute prefixed by cid:, look at this:

<?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文档)的描述以及您需要输出/存储的内容的一些示例.

I hope that gets you started. These are only examples of what to do with the DOM extension, your description of what you need to parse (HTML fragments or complete HTML document) and what you need to output/store were a bit vague.

这篇关于regexp在图像标签中查找图像路径文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆