class BbCode
{
private $string;
private $prepare = ['str' => [], 'preg' => []];
public function __construct($string)
{
$this->string = (string) trim(htmlentities($string));
}
public function prepare()
{
$this->profile()->bold()->italic()->underline()->through()->break()->line();
$this->string = str_replace($this->prepare['str']['search'], $this->prepare['str']['replace'], $this->string);
$this->string = preg_replace($this->prepare['preg']['pattern'], $this->prepare['preg']['replacement'], $this->string);
return $this;
}
private function break()
{
$this->prepare['str']['search'][] = '[br]';
$this->prepare['str']['replace'][] = '<br>';
return $this;
}
private function line()
{
$this->prepare['str']['search'][] = '[hr]';
$this->prepare['str']['replace'][] = '<hr>';
return $this;
}
private function profile()
{
$this->prepare['preg']['pattern'][] = '#\[profile=(\/profile\/\d+)\](.*)\[\/profile\]#is';
$this->prepare['preg']['replacement'][] = '<a href="$1">$2</a>';
return $this;
}
private function bold()
{
$this->prepare['preg']['pattern'][] = '#\[b\](.*)\[\/b\]#is';
$this->prepare['preg']['replacement'][] = '<b>$1</b>';
return $this;
}
private function italic()
{
$this->prepare['preg']['pattern'][] = '#\[i\](.*)\[\/i\]#is';
$this->prepare['preg']['replacement'][] = '<i>$1</i>';
return $this;
}
private function underline()
{
$this->prepare['preg']['pattern'][] = '#\[u\](.*)\[\/u\]#is';
$this->prepare['preg']['replacement'][] = '<u>$1</u>';
return $this;
}
private function through()
{
$this->prepare['preg']['pattern'][] = '#\[s\](.*)\[\/s\]#is';
$this->prepare['preg']['replacement'][] = '<s>$1</s>';
return $this;
}
public function __toString()
{
return $this->prepare()->string;
}
}