您尚未登录。

楼主 #1 2017-09-14 12:02:06

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,238
积分: 9197

一步一步调试,使网站增加 [video]标签功能

从 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">&lt;'.$lang_common['Image link'].' - '.$alt.'&gt;</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" />




离线

楼主 #2 2017-09-14 14:07:12

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,238
积分: 9197

Re: 一步一步调试,使网站增加 [video]标签功能

基本可以解析 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">&lt;'.$lang_common['Image link'].' - '.$alt.'&gt;</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;
?>




离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn