【发布时间】:2016-04-28 02:26:57
【问题描述】:
这就是我正在做的事情:
我的帖子只包含一个带有嵌入视频的短代码:
[video width="1280" height="720" mp4="http://myurl/wp-content/uploads/2016/01/VID_20160114_202806.mp4"][/video]
我想要的只是“mp4”变量,但它可能有不同的名称,具体取决于编码,所以假设我只需要 [2] 值。
请记住,上面的简码就是帖子中的全部内容。这是我的工作:
$atts = array();
$atts = shortcode_parse_atts( $post->post_content );
//then I use $atts[2];
这很失败。
我收到以下通知:Undefined offset: 2
如果我运行print_r,这是我得到的扭曲数组:
Array
(
[0] => [video
[width] => 1280
[height] => 720
[1] => mp4=" http:="" myurl="" wp-content="" uploads="" 2016="" 01="" vid_20160114_202806.mp4"][="" video]
(诚然,这是因为它被打印在一个 html 标记中而变得更糟,它解释了所有这些引号;关键是,参数的提取失败)
我认为$post->post_content 不是shortcode_parse_atts 想要的。它需要一串属性。但是,我如何获得属性字符串? (我知道,正则表达式,对吗?)
【问题讨论】:
-
是的,在这种情况下你可以使用正则表达式
标签: php arrays wordpress extract shortcode