我正在从旧博客中迁移大量内容,我需要它是XML格式的。问题是,我的旧网站没有包含图像网址的单独节点,而新网站也没有,所以我需要采用如下所示的XML节点:
<entry_text><img src="http://site.com/image.jpg" width="300" height="429" align="left" hspace="5" vspace="5" />Lorem Ipsum Dolor sit amet
</entry_text>
并使它看起来像这样:
<entry_text>Lorem Ipsum Dolor sit amet</entry_text>
<entry_image><img src="http://site.com/image.jpg" width="300" height="429" align="left" hspace="5" vspace="5" /></entry_image>
我发现了一个用于将这些变量存储在数组中的php函数,但是我对PHP不太了解如何在适当的位置创建XML节点:
$matches = array();
preg_match_all('!http://[^?#]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);
JQuery也可以运行。
答案 0 :(得分:1)
正则表达式:
\<entry_text\>(?<url>.*?/>)(?<text>.*?)\</entry_text\>
替换:
<entry_text>${text}</entry_text><entry_image>${url}</entry_image>
结果:
<entry_text>Lorem Ipsum Dolor sit amet</entry_text><entry_image><img src="http://site.com/image.jpg" width="300" height="429" align="left" hspace="5" vspace="5" /></entry_image>