Jump to content
Fórum Script Brasil
  • 0

Como se chama esta função?


MarlonDouglas27

Question

Oi gente. Eu já vi em alguns sites que guardam sites de usuários um efeito que funciona da seguinte maneira:

O link original é: http://www.youtube.com/watch?v=wPqmppP08wY...feature=channel

Mas, como ele é considerado grande pela função, ele fica mais ou menos assim:

http://www.youtub....nnel

É esta função que eu gostaria de saber o nome, alguém tem ideia? Agradeço pela ajuda de vocês.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

<?php
class UrlShrink
{
    const BEGIN_LENGTH = 17;
    const END_LENGTH = 4;
    const SEPARATOR = '....';

    /**
     * shrink
     * @param string $url An URL.
     * @return string Shrinked URL
     */
    public function shrink($url)
    {
        $urlLength = strlen($url);

        if ($this->shouldShrink($urlLength)) {
            $begin = substr($url, 0, self::BEGIN_LENGTH);
            $end = substr($url, ($urlLength - self::END_LENGTH));
            $url = $begin . self::SEPARATOR . $end;
        }

        return $url;
    }

    /**
     * shouldShrink
     * Should we shrink this URL?
     * @param int $urlLength URL length.
     * @return boolean
     */
    private function shouldShrink($urlLength)
    {
        return ($urlLength > (self::BEGIN_LENGTH + self::END_LENGTH));
    }
}
Uso:
(new UrlShrink())->shrink('http://www.youtube.com/watch?v=wPqmppP08wY...feature=channel'); // Resultado: http://www.youtub....nnel

Um abraço.

Edited by Willian Gustavo Veiga
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
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  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.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...