解析并替换包含附加属性的 WordPress 短代码
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用:
正则表达式中的单个
.
仅匹配一个字符。.*
贪婪地匹配任意数量的字符(即尽可能地匹配)。.*?
做同样的事情,但不是贪婪的(即,一旦下一部分或多或少匹配,它就会停止)。捕获部分是
[^"]*
,它是一个反向字符类,表示“除了”
重复零次或多次之外的任何内容”。并且您需要转义
[
和]
因为它们是字符类的开始/结束标记。如果您希望能够在
[thing]
之前或之后添加文本,请删除锚点(第一个^
和最后一个$
)。Try with:
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$
).好的,一些假设,因为这确实需要一个适当的解析器,而不是正则表达式:
gallery
标记中没有]
字符。gallery
标记中没有出现include=
这两种情况在引用的部分内部和外部都是正确的。
这个正则表达式应该可以工作:
$matches[1]
现在将包含一个以逗号分隔的字符串。$values
现在将是一个数字数组。OK, some assumptions, because this really needs a proper parser, rather than regex:
]
characters in yourgallery
tag.include=
in yourgallery
tagBoth of these are true inside quoted sections as much as outside them.
This regex should work:
$matches[1]
will now contain a comma-separated string.$values
will now be an array of the numbers.这可能行不通,因为他说画廊标签之前和之后可能有文本分别
包含在字符串开头和结尾的 ^ 和 $ 标志意味着该标记必须是一行中唯一的内容
Use:
并且它应该有效。
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:
and it should work.