Preg_replace 无法替换我需要的

发布于 2024-12-09 18:54:34 字数 1189 浏览 0 评论 0原文

我需要一些有关 preg_replace 的帮助。在我的网站上,我需要删除一些我不需要的东西。
这是一个例子:

[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]

好的,我需要从这个字符串中得到的是: caption="THIS TEXT" 中的文本, 我需要删除的所有其他内容,我已经使用过谷歌并尝试了一些示例,但没有任何结果。 也许我需要使用另一个函数,但从我在互联网上读到的内容来看,这应该取代 .

请帮助我,这非常重要。
谢谢。

编辑:

代码还有一些我忘记的事情。

[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]

所以我需要获取 CAPTION 文本 并删除其中的所有内容 [caption id="attachment_100951"align="alignleft"width="448"caption="eaaaaaaaaaaaaaaaaaa"]

<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>

也不想删除[/caption]

I need some help with preg_replace. At my website I need to delete some things I don't need.
Here is an example:

[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]

Ok, all I need from this string is: the text inside caption="THIS TEXT",
every thing else I need to be deleted, I have used Google and tried some examples but nothing.
Maybe I need to use another function, but from what I have read on the internet this should replace .

Please help me, it's very important.
Thank you.

EDIT:

The code has some other things that i have forgot.

[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]

So i need to get the CAPTION text and delete every thing in
[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]

but not

<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>

also want delete [/caption]

too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-12-16 18:54:34

此正则表达式:

$result = preg_replace('/\[caption\s+.*?caption\s*=(["\'])(.*?)\1.*?\[\/caption\]/', '$2', $subject);

将输出:

THIS IS WHAT I NEED

当应用于:

[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]

根据您更新的问题更新答案:

$result = preg_replace('%\[caption\s+.*?caption\s*=(["\'])(.*?)\1\s*\](.*?)\[/caption\]%s', '$2\n$3', $subject);

上面的正则表达式应用于:

[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]

将输出:

    eaaaaaaaaaaaaaaaaaa

<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>

我不确定这是否正是您想要的。当然,您可以使用正则表达式对组 $2 和 $3 进行匹配并执行任何您想要的操作...

This regex :

$result = preg_replace('/\[caption\s+.*?caption\s*=(["\'])(.*?)\1.*?\[\/caption\]/', '$2', $subject);

will output :

THIS IS WHAT I NEED

when applied to :

[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]

Updated answer based on your updated question :

$result = preg_replace('%\[caption\s+.*?caption\s*=(["\'])(.*?)\1\s*\](.*?)\[/caption\]%s', '$2\n$3', $subject);

The above regex applied to :

[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]

Will output :

    eaaaaaaaaaaaaaaaaaa

<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>

I am not sure if this is exactly what you wanted. Of course you can use the regex to match and do whatever you want with groups $2 and $3...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文