Get id of a particular URL inside the post and save it in a variable

后端 未结 2 413
走了就别回头了
走了就别回头了 2020-12-22 13:36

Inside my posts videos have code below:

[videojs mp4=\"http://www.example.com/video-play.mp4?contentId=7c6255ca14e901d2\" poster=\"https://2.bp.example.com/-         


        
相关标签:
2条回答
  • 2020-12-22 13:55

    In PHP, try use regular expression. You may have to adjust the regex if your sample does not always follow this format.

    $re = '/\[videojs mp4=\"(.*contentId=(.*?))\"/';
    $str = '[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"]';
    
    preg_match_all($re, $str, $matches);
    
    // Print the entire match result
    print_r($matches);
    

    And above result are supposed to be

    Array
    (
        [0] => Array
            (
                [0] => [videojs mp4="http://www.blogger.com/video-play.mp4?contentId=7c6255ca14e901d2"
            )
    
        [1] => Array
            (
                [0] => http://www.blogger.com/video-play.mp4?contentId=7c6255ca14e901d2
            )
    
        [2] => Array
            (
                [0] => 7c6255ca14e901d2
            )
    
    )
    
    0 讨论(0)
  • 2020-12-22 13:55

    Its an assumption that you get the whole [videojs mp4="uri" ---...] as string. Now, you wants to process it and assign it to several different variables for future use.

    $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];
        $id = explode('=',parse_url($url)['query'])[1];
        echo 'From Video string I get Id= '.$id . ' & url = '.$url;
    }
    
    0 讨论(0)
提交回复
热议问题