PHP正则表达式提取双方括号内的参数(Modx标签)

时间:2021-01-16 21:42:46

I'm trying to write a regex to extract the value of a particular parameter from an array of content in a modx database. the format of the tags is:

我正在尝试编写一个正则表达式,以从modx数据库中的内容数组中提取特定参数的值。标签的格式是:

[[!video? &path=`_path_to_video` &width=`123` &height=`123` &someotherparm=`bar`]] 

I am trying to get the content of the &path parameter using this regex:

我试图使用此正则表达式获取&path参数的内容:

preg_match_all('/\[\[path=`(.*?)`\]\]/', $content, $tags, PREG_PATTERN_ORDER) ;

but without luck - it just returns empty arrays when I dump the $tags variable.

但没有运气 - 当我转储$ tags变量时它只返回空数组。

something wrong with my regex?

我的正则表达式出了什么问题?

2 个解决方案

#1


1  

Your pattern doesn't match the tag format:

您的模式与标记格式不匹配:

preg_match_all('/&path=`([^`]+)`/', $content, $tags, PREG_PATTERN_ORDER);
  • Match &path= then backtick, then
  • 匹配&路径=然后反击,然后

  • Match anything that is not a backtick up until another backtick and capture it
  • 匹配任何不是反击的东西,直到另一个反击并捕获它

If you really need to match the existence of [[ and closing ]] then:

如果你真的需要匹配[[和结束]]的存在,那么:

preg_match_all('/\[\[.*&path=`([^`]+)`.*\]\]/', $content, $tags, PREG_PATTERN_ORDER);

#2


1  

Try this regex:

试试这个正则表达式:

'/\[\[.*?&path=`([^`]+?)`.*?\]\]/'

#1


1  

Your pattern doesn't match the tag format:

您的模式与标记格式不匹配:

preg_match_all('/&path=`([^`]+)`/', $content, $tags, PREG_PATTERN_ORDER);
  • Match &path= then backtick, then
  • 匹配&路径=然后反击,然后

  • Match anything that is not a backtick up until another backtick and capture it
  • 匹配任何不是反击的东西,直到另一个反击并捕获它

If you really need to match the existence of [[ and closing ]] then:

如果你真的需要匹配[[和结束]]的存在,那么:

preg_match_all('/\[\[.*&path=`([^`]+)`.*\]\]/', $content, $tags, PREG_PATTERN_ORDER);

#2


1  

Try this regex:

试试这个正则表达式:

'/\[\[.*?&path=`([^`]+?)`.*?\]\]/'