Skip to content

Instantly share code, notes, and snippets.

@einpraegsam
Created July 20, 2017 09:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einpraegsam/800b4ff1ebfd7b2b3a8c7bae237a590a to your computer and use it in GitHub Desktop.
Save einpraegsam/800b4ff1ebfd7b2b3a8c7bae237a590a to your computer and use it in GitHub Desktop.
Get youtube code from any URI
<?php
namespace In2code\In2template\Migration\Utility;
/**
* Class StringUtility
*/
class StringUtility
{
/**
* Parse every kind of Youtube URI and return video code
*
* Example URI:
* http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
* http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
* https://www.youtube.com/embed/6FjfewWAGdE
* https://youtube.com/embed/6FjfewWAGdE
* http://www.youtube.com/watch?v=6FjfewWAGdE
* http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage
* www.youtu.be/6FjfewWAGdE
* youtu.be/6FjfewWAGdE
* youtube.com/watch?v=6FjfewWAGdE
* https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be
*
* @param string $uri
* @return string
*/
public static function getYoutubeCodeFromUri(string $uri): string
{
$code = '';
$regExp = '~^(http://|https://|.*?)(www.|.*?)(youtube.com|youtu.be)/(embed/|watch\?v=|.*?)(.*?)(\?|\&|$)~';
preg_match($regExp, $uri, $result);
if (!empty($result[5])) {
$code = $result[5];
}
return $code;
}
}
@IchHabRecht
Copy link

improved regular expression:

$regExp = '~^(http://|https://)?(www\.)?(youtube\.com|youtu\.be)/(embed/|watch\?v=)?(?P<code>.*?)(\?|\&|$)~';
preg_match($regExp, $uri, $result);
if (!empty($result['code'])) {
    $code = $result['code'];
}

@IchHabRecht
Copy link

TestCase:

<?php
namespace In2code\In2template\Tests\Unit\Migration\Utility;

use In2code\In2template\Migration\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
 * Class StringUtility
 */
class StringUtilityTestCase extends UnitTestCase
{
    /**
     * @return array
     */
    public function getYoutubeCodeFromUriReturnsExpectedCodeDataProvider()
    {
        return [
            'http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage' => [
                'http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage',
                '6FjfewWAGdE'
            ],
            'https://www.youtube.com/embed/6FjfewWAGdE' => [
                'https://www.youtube.com/embed/6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'https://youtube.com/embed/6FjfewWAGdE' => [
                'https://youtube.com/embed/6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'http://www.youtube.com/watch?v=6FjfewWAGdE' => [
                'http://www.youtube.com/watch?v=6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage' => [
                'http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage',
                '6FjfewWAGdE'
            ],
            'www.youtu.be/6FjfewWAGdE' => [
                'www.youtu.be/6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'youtu.be/6FjfewWAGdE' => [
                'youtu.be/6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'youtube.com/watch?v=6FjfewWAGdE' => [
                'youtube.com/watch?v=6FjfewWAGdE',
                '6FjfewWAGdE'
            ],
            'https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be' => [
                'https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be',
                '6FjfewWAGdE'
            ],
        ];
    }

    /**
     * @test
     * @dataProvider getYoutubeCodeFromUriReturnsExpectedCodeDataProvider
     */
    public function getYoutubeCodeFromUriReturnsExpectedCode($uri, $expected)
    {
        $this->assertSame($expected, StringUtility::getYoutubeCodeFromUri($uri));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment