Created
April 26, 2017 01:20
-
-
Save davidchc/5ee70dd23764b8f4a604f77f5e12bd72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function video($value) | |
{ | |
if(preg_match("/youtube/i", $value[1])) | |
{ | |
return ' Youtube: '.$value[1]; | |
} | |
if(preg_match("/vimeo/i", $value[1])) | |
{ | |
return ' Vimeo: '.$value[1]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'SimpleShortcode.php'; | |
require_once 'functions.php'; | |
$shortcode = new SimpleShortcode(); | |
$shortcode->addTag('youtube', 'video'); | |
$shortcode->addTag('vimeo', 'video'); | |
$content = '[youtube]https://www.youtube.com/watch?v=YgHRBEYy6mM[/youtube]'; | |
$content .= '[vimeo]https://vimeo.com/57739087[/vimeo]'; | |
echo $shortcode->apply($content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleShortcode | |
{ | |
private $tags = []; | |
const regex = '/\[%s\](.*?)\[\/%s\]/'; | |
public function __construct($tags = []) | |
{ | |
$this->tags = $tags; | |
} | |
public function addTag($tag, $action) | |
{ | |
$this->tags[$tag] = $action; | |
} | |
public function apply($content) | |
{ | |
foreach($this->tags as $key => $value ) { | |
$regex = sprintf(self::regex, $key, $key); | |
if(preg_match($regex, $content)) { | |
$content = preg_replace_callback($regex, $value, $content); | |
} | |
} | |
return $content; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment