Jump to content

Recommended Posts

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";       
}

Link to comment
https://forums.phpfreaks.com/topic/243763-hep-shortening-a-piece-of-code/
Share on other sites

<?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.

 

i_heart_my_sensei_t_shirt-p235846181907006226qmkd_400.jpg

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.