Skip to content

Commit

Permalink
youtube url to embed
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Oct 26, 2023
1 parent bb5e365 commit 84672d4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ExportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 84672d4

Please sign in to comment.