Skip to content

Instantly share code, notes, and snippets.

@davidchc
Created April 26, 2017 01:20
Show Gist options
  • Save davidchc/5ee70dd23764b8f4a604f77f5e12bd72 to your computer and use it in GitHub Desktop.
Save davidchc/5ee70dd23764b8f4a604f77f5e12bd72 to your computer and use it in GitHub Desktop.
<?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];
}
}
<?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);
<?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