Php preg_如何替换?

Php preg_如何替换?,php,Php,所以我出现了一个问题。我有密码- [caption id="attachment222" align="aligncenter" width="520" caption="caption ghoes here"]<img class="image-caption" title="title" src="picture link goes here" alt="" width="520" height="328" />[/caption] [caption id=“attachmen

所以我出现了一个问题。我有密码-

[caption id="attachment222" align="aligncenter" width="520" caption="caption ghoes here"]<img class="image-caption" title="title" src="picture link goes here" alt="" width="520" height="328" />[/caption]
[caption id=“attachment222”align=“aligncenter”width=“520”caption=“caption ghoes here”][/caption]
因此,基本上我需要做的是浏览$content,获取每个
[caption id=“attachment222”align=“aligncenter”width=“520”caption=“caption ghoes here”url=“picture link goes here”][/caption]


如您所见,src也更改为url。希望您能帮我解决这个问题。

您的正则表达式可能类似于
\i
。我会让你算出剩下的部分。

这不准确,但它会为你指明正确的方向

[caption $1 src="$2"][/caption]
使用这样的正则表达式可以做两件事:获取标题数据和图像src

[caption (.*)]<img class="image-caption" title="title" src="(.*)" alt="" width="520" height="328" />[/caption]
$1是标题(.)中的内容,$2是图像src。如果[img]标记中的标题和其他属性是动态的,则只需在reg express中将它们替换为。像这样

[标题(.*)][/caption]
这应该行得通。(我讨厌正则表达式,所以可能有一种更优雅的方法。)

这应该可以:

$parsed = preg_replace(
    '{\[caption([^\]]+)\]<img.*?src=["\'](.*?)["\'].*?/>}mi',
    '[caption$1 url="$2"]',
    $sourceText
);
$parsed=preg\u replace(
“{\[标题([^\]+)\]}mi”,
“[标题$1 url=“$2”]”,
$sourceText
);

听起来好像有人需要学习正则表达式+1@anubhava,用于DOM@anubhava这在正则表达式中工作得非常好。DOM太笨重了,不适合做这个。。我怀疑它是否能很好地工作,因为括号中的元素很可能根本就不会成为DOM节点。谢谢,它工作得很有魅力,我想是时候开始学习正则表达式了
$parsed = preg_replace(
    '{\[caption([^\]]+)\]<img.*?src=["\'](.*?)["\'].*?/>}mi',
    '[caption$1 url="$2"]',
    $sourceText
);