获取url的id以回显到wordpress短代码

时间:2013-09-29 15:35:23

标签: php wordpress

我有一个网址http://api.spreaker.com/download/episode/3360726.mp3?preroll=0&commercials=0我想要做的就是在.mp3之前获取数字,这样我就可以将它添加到wordpress短代码中。网址来自RSS Feed。你可以看到这里的示例http://thecasadefe.com/category/blog/spreaker/现在它工作原因我手动输入短代码中的id但是从rss feed我得到url但是我无法通过php获取url的id以回显短代码。

这是我的代码:

    <?php $enclosure = get_post_meta($post->ID , 'enclosure', $single = true);
    $media=explode(chr(10),$enclosure); ?>
    <?php if (!is_null($media)) : ?>
    <?php $url='<a href="'.$media[0].'" rel="nofollow">'.$media[0].'</a>';
    echo($url); ?>
    <?php endif; ?>

    <?php $mp3title=the_title(); ?>
    <?php $url=''.$mp3title.'';echo($url); ?>
<?php echo do_shortcode("[spreaker type=standard width=100% autoplay=false episode_id=3161467]"); ?>

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

通过几个PHP函数parse_urlpathinfo解决了这个问题:

$url = 'http://api.spreaker.com/download/episode/3360726.mp3?preroll=0&commercials=0';
$parse = parse_url( $url );
/*
$parse = array (size=4)
  'scheme' => string 'http' (length=4)
  'host' => string 'api.spreaker.com' (length=16)
  'path' => string '/download/episode/3360726.mp3' (length=29)
  'query' => string 'preroll=0&commercials=0' (length=23)
*/

$info = pathinfo( $parse['path'] );
/*
$info = array (size=4)
  'dirname' => string '/download/episode' (length=17)
  'basename' => string '3360726.mp3' (length=11)
  'extension' => string 'mp3' (length=3)
  'filename' => string '3360726' (length=7)
*/
$final_id = $info['filename'];

答案 1 :(得分:0)

可能很顽皮并使用正则表达式

preg_match('~(\d+)(?=\.mp3)~',$url,$matches)

然后$ matches [0]将包含3360726

相关问题