从 parse.h 抽出 [ img ] 相关的代码出来:
<?php
function handle_img_tag($url, $is_signature = false, $alt = null)
{
global $lang_common, $pun_user;
if (is_null($alt))
$alt = basename($url);
$img_tag = '<a href="'.$url.'" rel="nofollow"><'.$lang_common['Image link'].' - '.$alt.'></a>';
if ($is_signature && $pun_user['show_img_sig'] != '0')
$img_tag = '<img class="sigimage" src="'.$url.'" alt="'.$alt.'" />';
else if (!$is_signature && $pun_user['show_img'] != '0')
$img_tag = '<span class="postimg"><img src="'.$url.'" alt="'.$alt.'" /></span>';
return $img_tag;
}
$pattern_callback[] = '%\[img\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%';
$pattern_callback[] = '%\[img=([^\[]*?)\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%';
$replace_callback[] = 'handle_img_tag($matches[1].$matches[3], true)';
$replace_callback[] = 'handle_img_tag($matches[2].$matches[4], true, $matches[1])';
$text = '[img]http://www.aa.com/c.png[/img]';
$count = count($pattern_callback);
for($i = 0 ; $i < $count ; $i++)
{
$text = preg_replace_callback($pattern_callback[$i], create_function('$matches', 'return '.$replace_callback[$i].';'), $text);
}
echo $text;
?>
运行结果:
# php test.php
<img class="sigimage" src="http://www.aa.com/c.png" alt="c.png" />
离线
基本可以解析 video 标签, 例如:
【video】http://www.aa.com/c.mp4【/video】
【video=test123】http://www.aa.com/dddd.mp4【/video】
<?php
function handle_img_tag($url, $is_signature = false, $alt = null)
{
global $lang_common, $pun_user;
if (is_null($alt))
$alt = basename($url);
$img_tag = '<a href="'.$url.'" rel="nofollow"><'.$lang_common['Image link'].' - '.$alt.'></a>';
if ($is_signature && $pun_user['show_img_sig'] != '0')
$img_tag = '<img class="sigimage" src="'.$url.'" alt="'.$alt.'" />';
else if (!$is_signature && $pun_user['show_img'] != '0')
$img_tag = '<span class="postimg"><img src="'.$url.'" alt="'.$alt.'" /></span>';
return $img_tag;
}
function handle_video_tag($url, $is_signature = false, $alt = null)
{
global $lang_common, $pun_user;
if (is_null($alt))
$alt = basename($url);
echo $url;
$video_tag = '<video id="my-video" class="video-js" controls preload="auto" width="720" height="600" poster="MY_VIDEO_POSTER.jpg" data-setup="{}">'. "\n";
$video_tag .= '<source src="' . $url . '" type="video/mp4">' . "\n";
$video_tag .= '</video>' . "\n";
return $video_tag;
}
$pattern_callback[] = '%\[img\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%';
$pattern_callback[] = '%\[img=([^\[]*?)\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%';
$pattern_callback[] = '%\[video\]((ht|f)tps?://)([^\s<"]*?)\[/video\]%';
$pattern_callback[] = '%\[video=([^\[]*?)\]((ht|f)tps?://)([^\s<"]*?)\[/video\]%';
$replace_callback[] = 'handle_img_tag($matches[1].$matches[3], true)';
$replace_callback[] = 'handle_img_tag($matches[2].$matches[4], true, $matches[1])';
$replace_callback[] = 'handle_video_tag($matches[1].$matches[3], true)';
$replace_callback[] = 'handle_video_tag($matches[2].$matches[4], true)';
$text = '[video]http://www.aa.com/c.mp4[/video] [video=test123]http://www.aa.com/dddd.mp4[/video]';
$count = count($pattern_callback);
for($i = 0 ; $i < $count ; $i++)
{
$text = preg_replace_callback($pattern_callback[$i], create_function('$matches', 'return '.$replace_callback[$i].';'), $text);
}
echo $text;
?>
离线