首页 >php教程 >PHP源码 >类微博功能设计

类微博功能设计

PHP中文网
PHP中文网原创
2016-05-25 17:10:451307浏览

php代码

<?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.&#39; &#39;; //防止最后有@,后面会去掉
		$pattarn = &#39;#@(.*)\s#U&#39;;
		$match = array();
		
		
		//获取用户昵称
		preg_match_all($pattarn , $content , $match);
		//构造查询条件
		$userIn = &#39;&#39;;
		foreach ($match[1] as $matchTemp){
			$userIn .= $matchTemp.&#39;,&#39;;
		}
		$userIn = substr($userIn, 0 , strlen($userIn)-1);
		$map[&#39;nicename&#39;]  = array(&#39;in&#39;,$userIn);
		
		//获取对用用户
		$this->userList = $userDb->field(&#39;account_id,nicename&#39;)->where($map)->select();
		
		//合并数组,方便操作 
		$userListGroup = array();  
		foreach($this->userList  as $userListTemp){
			$userListGroup[$userListTemp[&#39;account_id&#39;]] = $userListTemp[&#39;nicename&#39;];			
		}
		
		//生成替换格式
		if($isConstructUrl){
			$replaceArr = array();
			foreach($userListGroup as $account_id =>$nicename){
				$replace = $this->_constructUrl($account_id,$nicename);
				$replaceArr += array("@".$nicename.&#39; &#39; => $replace ); //这里空格也替换
			}
			//替换
			$content = strtr($content,$replaceArr);	
		}		
		//标签处理
		$tagPattarn = &#39;/#(.*)#/U&#39;;
		$tagMatch = array();
		//是否需要格式化标签
		if($isConstructTag && preg_match_all($tagPattarn , $content , $tagMatch) ){		
				$tagIn = &#39;&#39;;
				$tagReplacArr =  array();
				foreach ($tagMatch[1] as $tagTmp){
					if($constructTmp = $this->_constructTag($tagTmp)){
						$tagReplacArr += array(&#39;#&#39;.$tagTmp.&#39;#&#39; => $constructTmp);
					}
				}
				//替换
				$content = strtr($content,$tagReplacArr);
		}
		return 	$content;
	}
	
	/**
	 * 格式提及用户,格式未定。
	 * @param  $account_id 
	 * @param  $nicename
	 * @return string 返回格式的的用户链接
	 */
	private function _constructUrl($account_id,$nicename){
		return &#39;<a href="account_id/&#39;.$account_id.&#39;">@&#39;.$nicename.&#39; </a>&#39;;
	}
	
	/**
	 * 格式提及标签,格式未定。
	 * @param  $tagName 标签名
	 * @return boolean|string 返回格式的的标签
	 */
	private function _constructTag( $tagName ){
		/*不需查询数据库,
		 $tagDb = M(&#39;tages_info &#39;);
		$map[&#39;cnname&#39;] = $tagName;
		$map[&#39;enname&#39;] = $tagName;
		$map[&#39;_logic&#39;] = &#39;OR&#39;;
		if(! $tag = $tagDb->where($map)->field("tage_id")->find()){
			//没有该标签
			return false;
		}
		 */
		
		return &#39;<a href="search/&#39; . $tagName .&#39;">#&#39; . $tagName . &#39;#</a>&#39;;
	}
	
	/**
	 * 添加到提及数据库,暂未完成
	 */
	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);
	}
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn