如何从段落中剥离图像,然后将其移动到段落的开头

时间:2011-11-01 13:19:50

标签: php regex

我试图弄清楚如何使用正则表达式在一小段文本中移动图像,例如:

this is a <img src="image.jpg" /> paragraph

变成

<img src="image.jpg" /> this is a paragraph

有没有人有任何想法

1 个答案:

答案 0 :(得分:5)

简化匹配,但适用于仿XHTML:

 $src = preg_replace('/^(.*?)(<img[^>]+>)/m', '$2 $1', $src);
                       |   |    |                  |
           start of line   |    the img tag        switching
                         everything that follows   positions

这假设您将行的开头视为段落的开头。

由于这是你的第二个正则表达式问题,我想暗示阅读http://www.php.net/manual/en/reference.pcre.pattern.syntax.phphttp://regular-expressions.info/