Ricardo Silva Ricardo Silva - 1 month ago 8
PHP Question

Get id of a particular URL the post

I need to get any id after

http://www.example.com/video-play.mp4?contentId=XXXXXXXX
store id in a variable for later use

In each post is a different and a different video id

$video_string = '[videojs mp4="http://www.example.com/video-play.mp4?contentId=7c6255ca14e901d2" poster="https://2.bp.example.com/-lSTjYuDBiAQ/VvST8Z7z2OI/AAAAAAAAGPY/c8yAE675bLEMYI-OMwtauCiXeu1yZPZaw/s1600/fundososvideos.jpg" preload="none" controls="controls" width="100%" height="400"]';
if (preg_match('/mp4="(.*)"\sposter/', $video_string, $matches1)) {

$url = $matches1[1];
$id = explode('=',parse_url($url)['query'])[1];
echo 'From Video string I get Id= '.$id . ' & url = '.$url;
}

Answer

Try this

$video_string = '[videojs mp4="http://www.blogger.com/video-play.mp4?contentId=7c6255ca14e901d2" poster="https://2.bp.blogspot.com/-lSTjYuDBiAQ/VvST8Z7z2OI/AAAAAAAAGPY/c8yAE675bLEMYI-OMwtauCiXeu1yZPZaw/s1600/fundososvideos.jpg" preload="none" controls="controls" width="100%" height="400"]';
if (preg_match('/mp4="(.*)"\sposter/', $video_string, $matches1)) {

  $url = $matches1[1];
  $query_args = parse_url($url, PHP_URL_QUERY);
  $id = explode('contentId=', $query_args)[1];

  echo 'From Video string I get Id= '.$id . ' & url = '.$url;
}