Jump to content

preg_match returns Notice: Undefined offset


iamLearning

Recommended Posts

I understand the problem, but I do not know how to fix this.

 

 

I am using preg_match function here:

function youtube_id_from_url($link) {
    $pattern = 
        '%^# Match any youtube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www\.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu\.be/    # Either youtu.be,
        | youtube\.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch\?v=  # or /watch\?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
        $%x'
        ;
    $result = preg_match($pattern, $link, $matches);
    if (false !== $result) {
        return $matches[1];
    }else{
    return false;
}
}

Everything works with my code here as long as preg match finds a match. If it does not find a match it gives me this notice. How would I fix this?

<?php
include 'functions/common.php';
include 'dbconnect.php';


$link = $_POST['submit'];

//Start Youtube Video Submit Process
$youtubeid = (youtube_id_from_url($link)); //Get Youtube ID from URL

if (checkYoutubeOnline($youtubeid) == 1){
	
	   echo "The Youtube video seems to work fine The ID is ".$youtubeid
	
	}else{
		echo 'Sorry that Youtube video does not exist! Please check that your link is correct and that the video is still online.';
	}



?>

<form action="submit_video.php" method="post">
   <p>Link to video: <input type="text" name="link" /></p>
   <input type="submit" name="submit" value="Submit" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.