我正在从一个旧博客迁移一堆内容,我需要它是XML格式的。问题是,我的旧站点不包含用于图像url的单独节点,而新站点包含,因此我需要采用如下所示的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也可以。
Regex:
'<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>