diff --git a/CHANGELOG.md b/CHANGELOG.md index c935666..25af5b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/). +## 1.5.0 () + ++ Added new `StringHelper::toYouTubeEmbed()` function to extract YouTube links into an Embed links. + ## 1.4.3 (9. August 2023) + Fixed bug with wrong quoted regex in StringHelper::highlightWord() method. diff --git a/src/helpers/ExportHelper.php b/src/helpers/ExportHelper.php index 6f5cbf8..dfb15aa 100644 --- a/src/helpers/ExportHelper.php +++ b/src/helpers/ExportHelper.php @@ -154,7 +154,7 @@ protected static function generateOutputString(array $input, $delimiter) { $output = null; foreach ($input as $row) { - $output.= self::generateRow($row, $delimiter, '"'); + $output .= self::generateRow($row, $delimiter, '"'); } return $output; diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php index e026ae2..fa18b90 100644 --- a/src/helpers/StringHelper.php +++ b/src/helpers/StringHelper.php @@ -21,6 +21,25 @@ */ class StringHelper extends BaseStringHelper { + /** + * Convert a YouTube link to an Embedable Video URL. + * + * If the given input url is invalid, false is returned. + * + * @param string $url + * @return string|boolean + * @see https://stackoverflow.com/a/48130447 + */ + public static function toYouTubeEmbed($url) + { + preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match); + + if (isset($match[1])) { + return 'https://www.youtube.com/embed/' . $match[1]; + } + + return false; + } /** * TypeCast a string to its specific types. * @@ -275,7 +294,7 @@ public static function truncateMiddle($content, $word, $length, $affix = '..', $ // we could not find any match, therefore use casual truncate method. if ($first === false) { // as the length value in truncate middle stands for to the left and to the right, we multiple this value with 2 - return self::truncate($content, ($length*2), $affix); + return self::truncate($content, ($length * 2), $affix); } $last = $first + mb_strlen($word); diff --git a/tests/helpers/StringHelperTest.php b/tests/helpers/StringHelperTest.php index 70c0611..788da4a 100644 --- a/tests/helpers/StringHelperTest.php +++ b/tests/helpers/StringHelperTest.php @@ -7,6 +7,13 @@ class StringHelperTest extends HelpersTestCase { + public function testToYouTubeEmbed() + { + $this->assertSame('https://www.youtube.com/embed/NVcpJZJ60Ao', StringHelper::toYouTubeEmbed('https://www.youtube.com/watch?v=NVcpJZJ60Ao')); + $this->assertSame('https://www.youtube.com/embed/NVcpJZJ60Ao', StringHelper::toYouTubeEmbed('https://www.youtu.be/NVcpJZJ60Ao')); + $this->assertSame('https://www.youtube.com/embed/NVcpJZJ60Ao', StringHelper::toYouTubeEmbed('https://www.youtube.com/watch?time_continue=1&v=NVcpJZJ60Ao')); + } + public function testStringTypeCast() { $this->assertSame(0, StringHelper::typeCast("0"));