par
rimie » 07 avr. 2012, 07:53
voila la fonction:
<?php
function bbcode($text)
{
$tags = array(
'\[b\](.*?)\[\/b\]' => '<strong>$1</strong>',
'\[i\](.*?)\[\/i\]' => '<em>$1</em>',
'\[u\](.*?)\[\/u\]' => '<u>$1</u>',
); // closed array
foreach($tags as $k=>$v)
{
$text = preg_replace('/'.$k.'/', $v , $text);
}
return($text);
} // end function
?>
et voila la class:
<?php
final class BBCode {
public final function __construct($text) {
$this->text = $text;
$allowedTags = array(
'[b]' => '[/b]',
'[i]' => '[/i]',
'[u]' => '[/u]',
'[s]' => '[/s]',
);
$this->allowedTags = $allowedTags;
}
public final function bbcode()
{
$tags = array(
'\[b\](.*?)\[\/b\]' => '<strong>$1</strong>',
'\[i\](.*?)\[\/i\]' => '<em>$1</em>',
'\[u\](.*?)\[\/u\]' => '<u>$1</u>',
'\[s\](.*?)\[\/s\]' => '<s>$1</s>',
); // closed array
$this->tags = $tags;
foreach($this->tags as $k=>$v)
{
$this->newText = preg_replace('/'.$k.'/', $v , $this->text);
}
return($this->newText);
} // end function
} // end class
$f = new BBCode('Ceci est un [b]test GRAS[/b] et bien [u]souligne[/u], et ceci est un autre [b]test gras[/b] et voila [i]italic[/i][/b][/i][u]<br />');
echo $f->bbcode();
?>
merci
voila la fonction:
[php]
<?php
function bbcode($text)
{
$tags = array(
'\[b\](.*?)\[\/b\]' => '<strong>$1</strong>',
'\[i\](.*?)\[\/i\]' => '<em>$1</em>',
'\[u\](.*?)\[\/u\]' => '<u>$1</u>',
); // closed array
foreach($tags as $k=>$v)
{
$text = preg_replace('/'.$k.'/', $v , $text);
}
return($text);
} // end function
?>
[/php]
et voila la class:
[php]
<?php
final class BBCode {
public final function __construct($text) {
$this->text = $text;
$allowedTags = array(
'[b]' => '[/b]',
'[i]' => '[/i]',
'[u]' => '[/u]',
'[s]' => '[/s]',
);
$this->allowedTags = $allowedTags;
}
public final function bbcode()
{
$tags = array(
'\[b\](.*?)\[\/b\]' => '<strong>$1</strong>',
'\[i\](.*?)\[\/i\]' => '<em>$1</em>',
'\[u\](.*?)\[\/u\]' => '<u>$1</u>',
'\[s\](.*?)\[\/s\]' => '<s>$1</s>',
); // closed array
$this->tags = $tags;
foreach($this->tags as $k=>$v)
{
$this->newText = preg_replace('/'.$k.'/', $v , $this->text);
}
return($this->newText);
} // end function
} // end class
$f = new BBCode('Ceci est un [b]test GRAS[/b] et bien [u]souligne[/u], et ceci est un autre [b]test gras[/b] et voila [i]italic[/i][/b][/i][u]<br />');
echo $f->bbcode();
?>
[/php]
merci