shortysbest Posted August 4, 2011 Share Posted August 4, 2011 I'd like to get the same result, but in as little code as possible. Thanks (: //find link preg_match('`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i', $url, $found_url); $url = $found_url[0]; //found link //find website name preg_match('/.(\w+).com/', $url, $matches); $website = $matches[1]; if(!$website) { preg_match('/.(\w+).be/', $url, $matches); $website = $matches[1]; } //found website name #####################YOUTUBE############################# if($website=="youtube") { preg_match('/=([^&]+)/', $url, $video_img); $videoID = $video_img[1]; $thumbnail = "http://img.youtube.com/vi/".$videoID."/2.jpg"; } ####################YOUTU.BE############################ else if($website=="youtu") { preg_match('/youtu.be\/([^&]+)/', $url, $video_img); $videoID = $video_img[1]; $thumbnail = "http://img.youtube.com/vi/".$videoID."/2.jpg"; } Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/ Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 <?PHPSensei To The Rescue Line 1-21: Use the Ternary Operator inside the variables, will save you some lines of code (Reference: Comparison Operators) So What Can I Do Sensei? The code looks short enough, however if you want my opinion I would put this in a function that would take the url as arguments and spit out the desired based on your if/elseif statements there, which changes the same variables. This will be much cleaner, organized, and you won't get lost in your own code. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251608 Share on other sites More sharing options...
shortysbest Posted August 4, 2011 Author Share Posted August 4, 2011 I'm not really sure how that would help? Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251609 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Well if you took my advice and did some research you would know how it works. I can't post the entire re-written code there for you, I can give you reference to some techniques that may or may not help you shorten the code. Quoting yourself here I'd like to get the same result, but in as little code as possible. Thanks (: You can use the option I gave you, otherwise that code is short as it is. However I pointed that its messy, and you can get easily lost in that code if you decide to expand on it, make a function, much easier. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251610 Share on other sites More sharing options...
shortysbest Posted August 4, 2011 Author Share Posted August 4, 2011 Oh sorry, when i initially googled it I just checked the first link and it didn't seem to have anything, just the basic operators, Now I see how it would make it shorter. (this is also inside of a function like you said it should be) And well I guess what I was trying to accomplish by this post was maybe merging parts of the codes into one or something, aside from using ternary operators it seemed like there would be a shorter way of doing this. But thank you for your help thus far. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251613 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Some parts can be merged for example preg_match('/.(\w+).com/', $url, $matches); $website = $matches[1]; to $website = (preg_match('/.(\w+).com/', $url, $matches)) ? $matches[1] : false Also mark topic as solved, you'll know where to go from here. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251616 Share on other sites More sharing options...
shortysbest Posted August 4, 2011 Author Share Posted August 4, 2011 Yeah, thanks that did help a bit. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251621 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 You can do the same for preg_match('/youtu.be\/([^&]+)/', $url, $video_img); $videoID = $video_img[1]; and others. Quote Link to comment https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/#findComment-1251648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.