Jump to content

PHP and Regex revisiting and stuck


PNewCode
Go to solution Solved by PNewCode,

Recommended Posts

I'm attempting another go at this regex world and I feel like I'm reading something from mars. So I have 2 different codes that work for 2 different things. I'm hoping I can get some help with merging them. I stared at the regex101 for a few days now and I'm not getting it. 

Note: I didn't include any database stuff or connections and etc. I don't think it's relevant because the link is being sent just fine. I can't get it to convert.

Objecting: To sort of "Smush" the two regex together to be able to get the title from any youtube link type.

This one works for getting the title of a youtube video, if the video link is (for example)
https://www.youtube.com/watch?v=HQBG42Ggtac

However it will NOT work with videos withe a link like
https://youtu.be/HQBG42Ggtac?si=l2EE-4vqC9U5xIkj

 

<?php

    $link = $_POST['link']; ////// coming in from a form on a webpage as the user enters a youtube link


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $link);
        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

$document = htmlspecialchars($output);
curl_close($ch);     
        
$line = explode("\n", $document);
$judul = "";
foreach($line as $strline){

      preg_match('/\&lt;title\&gt;(.*?)\&lt;\/title\&gt;/s', $strline, $hasil);
      if (!isset($hasil[0]) || $hasil[0] == "") continue;
            $title =  str_replace(array("&lt;title&gt;", "&lt;/title&gt;"), "", $hasil[0]);

echo $title; ////// prints the video title on the screen
?>

HOWEVER...

The following will work when getting ALL links for getting the video thumbnail

    $link1 = $_POST['link']; ////// sent from a form on a different page


$ytvideo1 = $row['link'];

$url = "$ytvideo1";
parse_str( parse_url( $url, PHP_URL_QUERY ), $vid );
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];




echo "<img src='http://i4.ytimg.com/vi/$match[1]/mqdefault.jpg'>"; ////// shows the video thumbnail


Any guidance is massive appreciated. My few remaining hairs will thank you too as I wont pull the rest of them out trying to get this to work haha.

Link to comment
Share on other sites

5 hours ago, requinix said:

In the long run, it's easier not to try to have one regex handling a bunch of different possibilities. Just make two separately: one for youtube.com and one for youtu.be.

That would be a doable except unfortunately all of that string of things looks like a plate of spaghetti trying to be alphabet soup haha. I can't translate it enough to know how to add the "title" part to a different one, or how to make the current one for the title work with youtu.be
Also, there's the other issue when the "?si=" is added from the token shared link. I managed to make it all work for the thumbnail, but not the title. And honestly, that was done by stumbling upon the code on other sites.
I continue to look into the regex101 and other sites but my brain doesn't want to translate how to break apart all that string of stuff to understand it, to build onto it.

Link to comment
Share on other sites

23 minutes ago, requinix said:

If you want the title of a video then use their API. That's why it exists: so you don't have to pull down full user-facing pages and scrape them for pieces of information.

https://developers.google.com/youtube/v3/getting-started
https://developers.google.com/youtube/v3/quickstart/php

I saw those pages. Good tools I'm sure to use. I was hoping to try to make this work as I'm working on it though. Mainly because if I use their api and then run into this later for something else that needs regex, and there is no api for it (other than youtube for example) then I will be back to this step trying to make it work again. Most of what I do (this is mostly as a hobby, sometimes as a favor for friends) is to build, then learn from it. Even when I get clips of code from other sites, I'll often be able to see why it works, once it finally does work. Not so much the case with redex though. It's really tough to transate for me

Link to comment
Share on other sites

  • Solution

Update...

For anyone that comes here to find an answer, I was making some coffee and it hit me... I should define the first part, then make it a full url to strip the video id, and THEN put it in the rest to get the title. And after about 20 min of mangling it, this is the solution that works :)

 

$ytvideo1 = $link;

$linkurl = "$ytvideo1";
parse_str( parse_url( $linkurl, PHP_URL_QUERY ), $vid );
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $linkurl, $match);
$youtube_id = $match[1];

$preurl = "https://www.youtube.com/watch?v=$match[1]";

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $preurl);
        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

$document = htmlspecialchars($output);
curl_close($ch);     
        
$line = explode("\n", $document);
$judul = "";
foreach($line as $strline){

      preg_match('/\&lt;title\&gt;(.*?)\&lt;\/title\&gt;/s', $strline, $hasil);
      if (!isset($hasil[0]) || $hasil[0] == "") continue;
            $title =  str_replace(array("&lt;title&gt;", "&lt;/title&gt;"), "", $hasil[0]);

}



echo $title; ////// This shows the title of the video from any youtube link

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.