2016-01-21 62 views
0

因此,这里是我在做什么:我需要从一个简码提取变量,但我坚持

我有一个只包含一个嵌入式视频简码后:

[视频宽度= “1280” HEIGHT = “720” 的MP4 = “HTTP://myurl/wp-content/uploads/2016/01/VID_20160114_202806.mp4”] [/视频]

我要的是,“ 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想要的。它想要一串属性。但是,那么,我如何获得属性的字符串呢? (我知道,正则表达式,右)

+0

叶,你可以在这种情况下使用正则表达式 –

回答

2

这是简码应该如何设置:

function my_shortcode($atts){ 
    $atts = shortcode_parse_atts(array(
     //default values go here. 
    ), $atts); 
} 

现在,你应该正确地接收到的值,你打算和可$atts['width']$atts['height']$atts['mp4']访问它们。

0

谢谢Ohgodwhy,我意识到这是要走的路。但是它会产生问题,因为根据视频的编码,第三个值可能会有不同的名称。

但是我用正则表达式解决(我不想......因为懒惰......)

if (preg_match('%\[video \w+="\w+" \w+="\w+" \w+="(.*)"\]\[/video\]%', $post->post_content)) { 
     preg_match_all('%\[video \w+="\w+" \w+="\w+" \w+="(.*)"\]\[/video\]%', $post->post_content, $result); 

// now I can use $result[1][0] to get the video url; (I figured [1][0] by trial and error so don't ask why) 
相关问题