Parsing URLs to extract an ID
<p>I am currently extracting youtube video ids with the use of my function <code>getYoutubeId</code>. The function generally parses the link to find its respective ID. But I am having trouble assigning the input textbox a php variable that will then pass the <code>url</code> into the function and finally extract the ID. The video ID or Nothing at all is being echoed when pasting url into textbox. Here is an example: <a href="http://webprolearner2346.zxq.net/testYTvid/test.php" rel="nofollow">Mockup SITE</a></p>

<pre><code>    &lt;input type="text" name="youtube" value="&lt;? $sYoutubeUrl ?&gt;"&gt;
    &lt;input type="submit" value="Parsen"&gt;


    &lt;?

    function getYoutubeId($sYoutubeUrl) {

        # set to zero
        $youtube_id = "";
        $sYoutubeUrl = trim($sYoutubeUrl);

        # the User entered only the eleven chars long id, Case 1
        if(strlen($sYoutubeUrl) === 11) {
            $youtube_id = $sYoutubeUrl;
            return $sYoutubeUrl;
        }

        # the User entered a Url
        else {

            # try to get all Cases
            if (preg_match('~(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&amp;]v=)|youtu\.be/)([^"&amp;?/ ]{11})~i', $sYoutubeUrl, $match)) {
                $youtube_id = $match[1];
                return $youtube_id;
            }
            # try to get some other channel codes, and fallback extractor
            elseif(preg_match('~http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?|embed\/([0-9A-Za-z-_]{11})|watch\?v\=([0-9A-Za-z-_]{11})|#.*/([0-9A-Za-z-_]{11})~si', $sYoutubeUrl, $match)) {

                for ($i=1; $i&lt;=4; $i++) {
                    if (strlen($match[$i])==11) {
                        $youtube_id = $match[$i];
                        break;
                    }
                }
                return $youtube_id;
            }
            else {
                $youtube_id = "No valid YoutubeId extracted";
                return $youtube_id;
            }
        }
    }

   echo (getYoutubeId($sYoutubeUrl));

    ?&gt;
</code></pre>
