解析并替换包含附加属性的 WordPress 短代码

发布于 2024-11-09 10:55:58 字数 700 浏览 3 评论 0原文

我需要在 WordPress 帖子中搜索内容中包含图库的帖子,其中的照片是从媒体库插入的,而不是附加到帖子中的(这是 WordPress 部分,如果你不明白,没关系,这个只是为了解释我在做什么。

无论如何,$subject 看起来像这样:

"some text or no text at all [gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]some text or no text at all"

我需要的是包含的数字列表。请注意,

我尝试过的 其他参数(如顺序、链接等)不是强制性的。我自己,但我就是无法让它发挥作用,这就是我所做的:

$pattern = '/^.\[gallery.include="(?)".\]$/';
$subject = '[gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]';
var_dump(preg_match($pattern, $subject, $matches));
print_r($matches);

打印:

int(0) Array ( )

I need to search in wordpress posts for posts that have the gallery included in the content with the photos inserted from the media library and not attached to the posts (this is the wordpress part, if you don't get it, that's fine, this is just to explain what I'm doing.

Anyway, the $subject looks like this:

"some text or no text at all [gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]some text or no text at all"

What I need is the list of numbers from the include. Note that the other arguments (like order, link, etc) are not mandatory.

I tried myself but I just can't make it work, this is what I've done:

$pattern = '/^.\[gallery.include="(?)".\]$/';
$subject = '[gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]';
var_dump(preg_match($pattern, $subject, $matches));
print_r($matches);

Prints:

int(0) Array ( )

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

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

发布评论

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

评论(3

慕巷 2024-11-16 10:55:58

尝试使用:

$pattern = '/^\[gallery.*?\sinclude="([^"]*)".*\]$/';

正则表达式中的单个 . 仅匹配一个字符。 .* 贪婪地匹配任意数量的字符(即尽可能地匹配)。 .*? 做同样的事情,但不是贪婪的(即,一旦下一部分或多或少匹配,它就会停止)。

捕获部分是 [^"]*,它是一个反向字符类,表示“除了 重复零次或多次之外的任何内容”。

并且您需要转义 [] 因为它们是字符类的开始/结束标记。

如果您希望能够在 [thing] 之前或之后添加文本,请删除锚点(第一个 ^ 和最后一个 $)。

Try with:

$pattern = '/^\[gallery.*?\sinclude="([^"]*)".*\]$/';

A single . in a regex only matches one character. .* matches any number of characters, greedily (i.e. it goes as far as it can). .*? does the same thing, but non-greedily (i.e. it stops as soon as the next part gets a match more or less).

The capture part is [^"]* which is an inverted character class which means "anything but a " repeated zero or more times".

And you need to escape [ and ] because they are the start/end markers for character classes.

If you want to be able to have text before or after the [thing], remove the anchors (first ^, and last $).

怀念你的温柔 2024-11-16 10:55:58

好的,一些假设,因为这确实需要一个适当的解析器,而不是正则表达式:

  • 您的 gallery 标记中没有 ] 字符。
  • gallery 标记中没有出现 include=

这两种情况在引用的部分内部和外部都是正确的。

这个正则表达式应该可以工作:

$pattern = '/\[gallery[^\]]* include="([\d,]*)"/';

$matches[1] 现在将包含一个以逗号分隔的字符串。

$values = explode(',', $matches[1]);

$values 现在将是一个数字数组。

OK, some assumptions, because this really needs a proper parser, rather than regex:

  • there are no ] characters in your gallery tag.
  • there's no occurrence of the include= in your gallery tag

Both of these are true inside quoted sections as much as outside them.

This regex should work:

$pattern = '/\[gallery[^\]]* include="([\d,]*)"/';

$matches[1] will now contain a comma-separated string.

$values = explode(',', $matches[1]);

$values will now be an array of the numbers.

不奢求什么 2024-11-16 10:55:58

尝试:

$模式=
'/^[画廊。?include="([^"])".*]$/';

这可能行不通,因为他说画廊标签之前和之后可能有文本分别

包含在字符串开头和结尾的 ^ 和 $ 标志意味着该标记必须是一行中唯一的内容

Use:

/\[gallery.*?include="([^"]*?)".*?\]/

并且它应该有效。

Try with:

$pattern =
'/^[gallery.?include="([^"])".*]$/';

That probably won't work because he said there might be text before and after the gallery tags.

The ^ and $ flags contained respectivelly at the start and end of your string means that this tag must be the only thing in a line.

Use:

/\[gallery.*?include="([^"]*?)".*?\]/

and it should work.

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