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
3 Answers
Reset to default 3There'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
- For you, you'd get
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;
}