在xml节点中提取图像URL并将它们放在新节点中

时间:2011-04-08 16:53:49

标签: php xml regex migration

我正在从旧博客中迁移大量内容,我需要它是XML格式的。问题是,我的旧网站没有包含图像网址的单独节点,而新网站也没有,所以我需要采用如下所示的XML节点:

<entry_text>&lt;img src=&quot;http://site.com/image.jpg&quot; width=&quot;300&quot; height=&quot;429&quot; align=&quot;left&quot; hspace=&quot;5&quot; vspace=&quot;5&quot; /&gt;Lorem Ipsum Dolor sit amet
</entry_text>

并使它看起来像这样:

<entry_text>Lorem Ipsum Dolor sit amet</entry_text>
<entry_image>&lt;img src=&quot;http://site.com/image.jpg&quot; width=&quot;300&quot; height=&quot;429&quot; align=&quot;left&quot; hspace=&quot;5&quot; vspace=&quot;5&quot; /&gt;</entry_image>

我发现了一个用于将这些变量存储在数组中的php函数,但是我对PHP不太了解如何在适当的位置创建XML节点:

$matches = array();
preg_match_all('!http://[^?#]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);

JQuery也可以运行。

1 个答案:

答案 0 :(得分:1)

正则表达式:

\<entry_text\>(?<url>.*?/&gt;)(?<text>.*?)\</entry_text\>

替换:

<entry_text>${text}</entry_text><entry_image>${url}</entry_image>

结果:

<entry_text>Lorem Ipsum Dolor sit amet</entry_text><entry_image>&lt;img src=&quot;http://site.com/image.jpg&quot; width=&quot;300&quot; height=&quot;429&quot; align=&quot;left&quot; hspace=&quot;5&quot; vspace=&quot;5&quot; /&gt;</entry_image>