首页 > php教程 > PHP源码 > 正文

类微博功能设计

PHP中文网
发布: 2016-05-25 17:10:45
原创
1517人浏览过

php代码

燕雀光年
燕雀光年

一站式AI品牌设计平台,支持AI Logo设计、品牌VI设计、高端样机设计、AI营销设计等众多种功能

燕雀光年 66
查看详情 燕雀光年
<?php 
/**
 *MentionModel.class.php
 */
class mentionModel extends Model{
	private $userList; //存放最近一次提取到提及用户列表
	
	/**
	 * 提取@信息,添加链接,并插入mention数据库
	 * @param  $content 需要提取的文本
	 * @param  $isConstructUrl,是否构造url,转发的话不需要再次构造url
	 * @param  $isConstructTag,是否构造Tag
	 * @return string 返回构造好的文本
	 */
	public  function convertContent($content  ,$isConstructUrl = TRUE ,$isConstructTag = TRUE){
		$mentionDb = M("mention");
		$userDb = M("account_users");

		
		$content = $content.' '; //防止最后有@,后面会去掉
		$pattarn = '#@(.*)\s#U';
		$match = array();
		
		
		//获取用户昵称
		preg_match_all($pattarn , $content , $match);
		//构造查询条件
		$userIn = '';
		foreach ($match[1] as $matchTemp){
			$userIn .= $matchTemp.',';
		}
		$userIn = substr($userIn, 0 , strlen($userIn)-1);
		$map['nicename']  = array('in',$userIn);
		
		//获取对用用户
		$this->userList = $userDb->field('account_id,nicename')->where($map)->select();
		
		//合并数组,方便操作 
		$userListGroup = array();  
		foreach($this->userList  as $userListTemp){
			$userListGroup[$userListTemp['account_id']] = $userListTemp['nicename'];			
		}
		
		//生成替换格式
		if($isConstructUrl){
			$replaceArr = array();
			foreach($userListGroup as $account_id =>$nicename){
				$replace = $this->_constructUrl($account_id,$nicename);
				$replaceArr += array("@".$nicename.' ' => $replace ); //这里空格也替换
			}
			//替换
			$content = strtr($content,$replaceArr);	
		}		
		//标签处理
		$tagPattarn = '/#(.*)#/U';
		$tagMatch = array();
		//是否需要格式化标签
		if($isConstructTag && preg_match_all($tagPattarn , $content , $tagMatch) ){		
				$tagIn = '';
				$tagReplacArr =  array();
				foreach ($tagMatch[1] as $tagTmp){
					if($constructTmp = $this->_constructTag($tagTmp)){
						$tagReplacArr += array('#'.$tagTmp.'#' => $constructTmp);
					}
				}
				//替换
				$content = strtr($content,$tagReplacArr);
		}
		return 	$content;
	}
	
	/**
	 * 格式提及用户,格式未定。
	 * @param  $account_id 
	 * @param  $nicename
	 * @return string 返回格式的的用户链接
	 */
	private function _constructUrl($account_id,$nicename){
		return '<a href="account_id/'.$account_id.'">@'.$nicename.' </a>';
	}
	
	/**
	 * 格式提及标签,格式未定。
	 * @param  $tagName 标签名
	 * @return boolean|string 返回格式的的标签
	 */
	private function _constructTag( $tagName ){
		/*不需查询数据库,
		 $tagDb = M('tages_info ');
		$map['cnname'] = $tagName;
		$map['enname'] = $tagName;
		$map['_logic'] = 'OR';
		if(! $tag = $tagDb->where($map)->field("tage_id")->find()){
			//没有该标签
			return false;
		}
		 */
		
		return '<a href="search/' . $tagName .'">#' . $tagName . '#</a>';
	}
	
	/**
	 * 添加到提及数据库,暂未完成
	 */
	public  function addToMention($type , $id){
		if(! $this->userList || !$type || !$id){
			return false;
		}
		dump($this->userList);
	}
}



class TestAction extends Action {
	public function index(){
		$mentionDb = D("mention");
		$content = "#百搭#@ @小红    @小明   @sdfklj  @dsf  #英伦##英伦##英伦#  #english##英伦##sdf#";
		//构造content
		$content =  $mentionDb->convertContent($content , true , true);
		echo $content;
		//......将分享或评论什么的插入数据库,并获取其id $id与对应类别 $type	

		$type = 2; //假设类别为2
		$id = 1;//假设插入数据库的分享或评论的id为1
		$mentionDb->addToMention($type,$id);
	}
}
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号