Skip to content

Instantly share code, notes, and snippets.

@nokimaro
Forked from jbowens/ImageWithClass.php
Last active August 29, 2015 14:11
Show Gist options
  • Save nokimaro/7849c6f2e9d23e5751fc to your computer and use it in GitHub Desktop.
Save nokimaro/7849c6f2e9d23e5751fc to your computer and use it in GitHub Desktop.
<?php
require_once "jBBCode" . DIRECTORY_SEPARATOR . "Parser.php";
/**
* Implements an [img=alt] tag that supports an optional class argument.
*
*/
class ImageWithClass extends \JBBCode\CodeDefinition
{
public function __construct()
{
$this->parseContent = false;
$this->useOption = true;
$this->setTagName('img');
$this->nestLimit = -1;
}
public function asHtml(\JBBCode\ElementNode $el)
{
$url = '';
foreach ($el->getChildren() as $child) {
$url .= $child->getAsText();
}
// Split the argument on the pipe character
$argPieces = explode('|', $el->getAttribute());
$altText = $argPieces[0];
$class = 'default-class';
if (count($argPieces) > 1) {
$class = $argPieces[1];
}
return '<img src="' + $url + '" alt="' + $altText + '" class="' + $class + '" />';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment