PHP将<img/>标记转换为自定义XML

时间:2017-01-29 08:55:05

标签: php html xml

我自己教PHP用于一个小项目。我需要将源HTML文件(可能有很多)中的所有标记转换为自定义XML。我一直在尝试使用DOMDocument类,但似乎无法正常工作。

举个例子,我需要转换

<img class="alignnone size-large wp-image-23904" src="https://picnic.ly/wp-content/uploads/2017/01/Screen-Shot-2560-01-27-at-2.32.06-PM-1024x572.png" alt="this is a picture" width="1024" height="574" />

<image>
<description>VALUE FROM ALT</description>
<url>VALUE FROM SRC</url>
</image>

希望对此有所帮助......先谢谢!

2 个答案:

答案 0 :(得分:2)

使用以下代码获取XML字符串:

<?php
// We use dom document to load it as an php object
$document = new DOMDocument();
$document->loadHTML('<img class="alignnone size-large wp-image-23904" src="https://picnic.ly/wp-content/uploads/2017/01/Screen-Shot-2560-01-27-at-2.32.06-PM-1024x572.png" alt="this is a picture" width="1024" height="574" />');
$img = $document->getElementsByTagName("img")->item(0);
// The Wrapper for your xml
$xml = "<image>\n";
for ($i = 0; $i < $img->attributes->length; $i++) {
    $attribute = $img->attributes->item($i);
    $name = $attribute->name;
    $value = $attribute->textContent;
    // Indent the element
    $xml .= "    ";
    // Create the element
    $xml .= "<" . $name . ">";
    $xml .= $value;
    $xml .= "</" . $name . ">";
    // Break line at end
    $xml .= "\n";
}
$xml .= "</image>";
echo $xml;

结果:

<image>
    <class>alignnone size-large wp-image-23904</class>
    <src>https://picnic.ly/wp-content/uploads/2017/01/Screen-Shot-2560-01-27-at-2.32.06-PM-1024x572.png</src>
    <alt>this is a picture</alt>
    <width>1024</width>
    <height>574</height>
</image>

告诉我这不是您想要的解决方案或有问题。

编辑:最佳解决方案是我创建的http://syframework.alwaysdata.net/44j

答案 1 :(得分:-1)

<image>
  <src>the url to the image</src>
  <alt>alt_Description_</alt>
  <description>Add_Image_description</description>
  <class>Image_Class_Add_</class>
  <height>Image_height_in_pixels</height>
  <width>Image_width_in_pixels</width>
  <title>Whatever_title_you_want_for_the_image</title>
</image>