Extracting YouTube video information using PHP

Hey everyone! I’m trying to get some specific info from YouTube video links. I’ve got this long string with video details and I need to pull out four things:

  1. The first number (like 34)
  2. The first URL (it’s pretty long)
  3. The second number (like 5)
  4. The second URL (also long)

I tried using this regex before:

if (preg_match_all('#|(.*?),#', $video_info, $matches)) {
    // Do something with $matches
}

But it’s not working quite right. Any ideas on how to split this up better? Maybe there’s a smarter way to do it without regex?

Thanks for any help! Sorry if my explanation isn’t great, still learning!

yo gizmo, have u tried using preg_split()? it’s like a mix of regex and explode. here’s a quick example:

$parts = preg_split(‘/,(?=\d+,|http)/’, $video_info);
$first_num = $parts[0];
$first_url = $parts[1];
$second_num = $parts[2];
$second_url = $parts[3];

this should work even if urls have commas. good luck!

I’ve encountered a similar issue when working with YouTube data. While explode() can work, it might not be reliable if the URLs contain commas. Here’s an approach that’s served me well:

Use strpos() to find the positions of the commas, then substr() to extract the parts. Something like:

$pos1 = strpos($video_info, ‘,’);
$pos2 = strpos($video_info, ‘,’, $pos1 + 1);
$pos3 = strpos($video_info, ‘,’, $pos2 + 1);

$first_num = substr($video_info, 0, $pos1);
$first_url = substr($video_info, $pos1 + 1, $pos2 - $pos1 - 1);
$second_num = substr($video_info, $pos2 + 1, $pos3 - $pos2 - 1);
$second_url = substr($video_info, $pos3 + 1);

This method is more precise and handles potential edge cases better than regex or simple explode().

I’ve found that using preg_match() with a carefully crafted regex pattern can be quite effective for this task. Here’s an approach that’s worked well for me:

$pattern = ‘/^(\d+),(.+?),(\d+),(.+)$/’;
if (preg_match($pattern, $video_info, $matches)) {
$first_num = $matches[1];
$first_url = $matches[2];
$second_num = $matches[3];
$second_url = $matches[4];
}

This pattern matches the structure you described: two numbers separated by URLs. It’s robust enough to handle commas within the URLs themselves. Just make sure your $video_info string is properly formatted.

If you’re dealing with a large volume of data, consider looking into YouTube’s API documentation. They often provide more structured data formats which could simplify your parsing process significantly.

hey gizmo, have u tried using explode() instead of regex? might be easier. something like:

$parts = explode(‘,’, $video_info);
$first_num = $parts[0];
$first_url = $parts[1];
$second_num = $parts[2];
$second_url = $parts[3];

just an idea, hope it helps!

I’ve dealt with YouTube data parsing before, and I found that using a combination of regular expressions and string manipulation works best. Here’s an approach that’s been reliable for me:

$pattern = '/^(\d+),(.+?),(\d+),(.+)$/';
if (preg_match($pattern, $video_info, $matches)) {
    $first_num = $matches[1];
    $first_url = $matches[2];
    $second_num = $matches[3];
    $second_url = $matches[4];
}

This regex is more specific to your data structure. It looks for two numbers separated by URLs, which should match your description. The ^ and $ ensure we’re matching the whole string, reducing false positives.

If you’re dealing with a large number of videos, you might want to consider using a more robust parsing method, like JSON or XML, depending on how you’re receiving the data from YouTube’s API. It could save you headaches down the line as your project grows.