$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Get URL from shortcode tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Get URL from shortcode tag

matteradmin7PV0评论

I am running a wordpress site and I need the correct regex syntax to get a URL from some shortcode that is returned inside the_content().

When I use the_content(), it will return something like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nulla enim, euismod ut pharetra nec, commodo a augue. Etiam sit amet nibh mauris, eu ornare purus. Vestibulum sed sem enim, sit amet congue leo. 

[video mp4=<<insert video url here>> poster=<<insert imgae placeholder url>>]

How can I get just the video mp4 URL?

I am running a wordpress site and I need the correct regex syntax to get a URL from some shortcode that is returned inside the_content().

When I use the_content(), it will return something like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nulla enim, euismod ut pharetra nec, commodo a augue. Etiam sit amet nibh mauris, eu ornare purus. Vestibulum sed sem enim, sit amet congue leo. 

[video mp4=<<insert video url here>> poster=<<insert imgae placeholder url>>]

How can I get just the video mp4 URL?

Share Improve this question edited Jan 16, 2013 at 0:03 shea 5,6724 gold badges39 silver badges62 bronze badges asked Nov 17, 2012 at 5:28 Timothy BassettTimothy Bassett 111 bronze badge 1
  • This isn't a WordPress question, IMHO. If reworded, it could be though. – Dan Commented Jan 16, 2013 at 0:09
Add a comment  | 

3 Answers 3

Reset to default 3

There's no need to use regex for this. WordPress has a Shortcode API to do this work for you.

That codex page includes one of my favorite codex code snippets:

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    ...

Right at the start of the function registering the shortcode it does the following:

  • take the user-submitted shortcodes and fill them in with defaults if they aren't provided
  • takes the $atts argument and turn each valued into a variable (e.g. $foo, $bar, etc.)
    • For you, you'd get $mp4 and $poster

Catch the mp4 url with this:

<?php
    $text = get_the_content(); //untested with get_the_content()
    preg_match_all("\bmp4="(.+)\b", $text, $matches);
    var_dump($matches[0]);
?>

Eg. [video mp4="http://somevideo/abc"], would output:

http://somevideo/abc

This is my Gist, the example in it works for me, but results might differ depending on $text.

You can use substring and strpos to extract this

$spos = strpos($getContent,"mp4=");
if($spos) {
$spos = $spos+4;//4 for four charactars mp4= 
$epos = strpos($getContent,' ',$spos) - $spos;
$url = substr($getContent,$spos,$epos);
echo $url;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far