nieco inaczej to zrobiłem
oto prościutki kodzik:
<?php
class micropost {
static private $replace = false;
private function __construct( $string, $config=false ) {
'string' => trim($string), 'config' => $config
);
}
public function parse( $string, $config=false ) {
if( !self::$post ) {
self::$post = new self( $string, $config );
}
$email = '{([a-z0-9\.\-_]+@[a-z\.\-_]+\.[a-z]{2,4})}';
$string = self::email( $arr[0], $string );
$gg = '{gg:([0-9]*)}';
$string = self::gg( $arr[1], $string );
$url = '{(http|https|ftp)(://)([a-zA-Z0-9\.-_/#]*)}';
$string = self::url( $arr[0], $string );
$toUser = '/>([a-zA-Z0-9\-_]*):/sUe';
$string = preg_replace($toUser, 'self::toUser(\'\\1\')', $string);
$getUser = '/\^([a-zA-Z0-9\-_]*)([^a-zA-Z0-9\-_])/sUe';
$string = preg_replace($getUser, 'self::getUser(\'\\1\', \'\\2\')', $string);
$tags = '/#([a-zA-Z0-9\-_]*)([^a-zA-Z0-9\-_])/sUe';
$string = preg_replace($tags, 'self::tags(\'\\1\', \'\\2\')', $string);
return self::reparse($string);
}
private function reparse( $string ) {
if( !self::$post ) {
self::$post = new self( $string, $config );
}
foreach( self::$post->replace as $key => $value ) {
if( $test[1] == 'Email') {
$url = '<a href="' . $value . '">[email]</a>';
} elseif( $test[1] == 'Url') {
$url = '<a href="' . $value . '">[link]</a>';
} elseif( $test[1] == 'GG') {
$url = '<a href="gg:' . $value . '">[gg]</a>';
}
}
return $string;
}
private function toUser( $user ) {
if( !isset(self::$post->post['config']['toUser']) ) { return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . $user . '">>' . $user . ':</a>';
} else {
return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . $user . '" ' . self::$post->post['config']['toUser'] . '>>' . $user . ':</a>';
}
}
private function getUser( $user, $separator ) {
if( !self::$post ) {
self::$post = new self( $string, $config );
}
if( !isset(self::$post->post['config']['toUser']) ) { return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . mb_strtoupper($user) . '">^' . $user . '</a>' . $separator;
} else {
return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . mb_strtoupper($user) . '" ' . self::$post->post['config']['toUser'] . '>^' . $user . '</a>' . $separator;
}
}
private function tags( $tag, $separator ) {
if( !self::$post ) {
self::$post = new self( $string, $config );
}
self::$post->post['tags'][] = $tag;
if( !isset(self::$post->post['config']['tags']) ) { return '<a href="/tagi/' . $tag . '.html" title="#' . mb_strtoupper($tag) . ' - statusy oznaczone tagiem ' . mb_strtoupper($tag) . '">#' . $tag . '</a>' . $separator;
} else {
return '<a href="/tagi/' . $tag . '.html" title="#' . mb_strtoupper($tag) . ' - statusy oznaczone tagiem ' . mb_strtoupper($tag) . '" ' . self::$post->post['config']['tags'] . '>#' . $tag . '</a>' . $separator;
}
}
private function email( $email, $string ) {
if( !self::$post ) {
self::$post = new self( $string, $config );
}
foreach( $email as $value ) {
$key = 'keyEmail_' . md5($value); self::$post->replace[$key] = 'mailto:' . $value;
}
return $string;
}
private function url( $url, $string ) {
foreach( $url as $value ) {
$key = 'keyUrl_' . md5($value); self::$post->replace[$key] = $value;
}
return $string;
}
private function gg( $gg, $string ) {
foreach( $gg as $value ) {
$key = 'keyGG_' . md5($value); self::$post->replace[$key] = $value;
$string = str_replace( 'gg:' . $value, $key, $string ); }
return $string;
}
}
'toUser' => 'style="font-weight: bold;"',
'link' => 'style="color: red;"',
'email' => 'style="color: yellow;"',
'tags' => 'style="color: #0ff;"'
);
$post = '
>sniver: oglądam #internet i czytam: <a href="http://www.youpc.pl/faq.html#blebleble" target="_blank">http://www.youpc.pl/faq.html#blebleble</a> napisz na: sniver@xyz.com lub gg:123, albo PW: ^sniver
';
echo trim(micropost
::parse( $post, $config ));
?>