Jump to content

Php and Preg Match


PNewCode

Recommended Posts

Hello. I have the following that works great for youtube links. I'm using this to show a thumbnail with the help of some other coding that works awesome. However there needs something added. I tried to alter this in many various way but I don't seem to get a grasp on how to split it up right to add to it. The following is what I have.

$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];

And it works for ALMOST all of the youtube links. However if I have one that is like 
 

Quote



Then it doesn't work. I tried to add the "si" in there in multiple ways but not sure how. This is the only type of link that is stumping me so far

Any thoughts on how to alter what I have to make it work?


PS, I know it's adding my examples as links. I'm sorry. I couldn't find how to add those without it auto linking.

Link to comment
Share on other sites

As far as it could be an error, try adding ini_set('display_errors', '1'); and error_reporting(E_ALL); at the top of your file.

Also, regex101.com is a great website for debugging why the regex isn't working. It also has good documentation.

Link to comment
Share on other sites

@Andou thank you, however The regex I have now works. I'm not getting any errors. But it's not doing what it needs to do because some links are like the example I gave instead of the other types. the "si" part is what needs to be added and those are also more than 11 characters

I looked up a lot of docs on regex and the link you gave too. But that will have to be for when I can learn from scratch instead of just adding to what I have. Though I fully plan to do that asap

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.