0

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?

  • This isn't a WordPress question, IMHO. If reworded, it could be though. – Dan Jan 16 '13 at 0:09
2

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
1

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.com/abc"], would output:

http://somevideo.com/abc

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

  • your preg_match_all is giving a syntax error but looks like a nice idea. – Gazi Nov 13 '18 at 11:36
  • This is my Gist, the example in it works for me, but results might differ depending on $text. – The J Nov 15 '18 at 15:53
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.