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


How to strip an image from a paragraph, and then move it to the beginning of paragraph

我正试图找出如何在一小段文本中移动图像使用正则表达式,以便例如:

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

变成

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

有人有什么想法吗

简化匹配,但也适用于伪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.php和http://regular-expressions.info/