duanqiang7631 2013-02-19 05:30
浏览 45
已采纳

如何在php中增加这段代码的性能?

How increase the performance of this code in php?
or any alternative method to find out the comment if the string starts with @ then call at() or if string starts with "#" then call hash()

here the sample comment is "#hash @at ###@@tag";

/

/this is the comment with mention,tag
    function getCommentWithLinks($comment="@name #tag @#@nam1 test", $pid, $img='', $savedid='', $source='', $post_facebook='', $fbCmntInfo='') {

    //assign to facebook facebookComment bcz it is used to post into the fb wall
    $this->facebookComment  =   $comment;

    //split the comment based on the space
    $comment        =   explode(" ", $comment);
    //get the lenght of the splitted array
    $cmnt_length    =   count($comment);
    $store_cmnt     =   $tagid  =   '';
    $this->img      =   $img;
    $this->saveid   =   $savedid;//this is uspid in product saved table primary key

    //$this->params = "&product=".base_url()."product/".$this->saveid;
    $this->params['product'] = base_url()."product/".$this->saveid;
    //$this->params['tags']='';

    foreach($comment as $word) {
        //check it is tag or not 
        //the first character must be a # and remaining all alphanumeric if any # or @ is exist then it is comment
        //find the length of the tag @mention
        $len    =   strlen($word);
        $cmt    =   $c  =   $tag_name   =   '';
        $j      =   0;
        $istag  =   false;

        for($i=0; $i<$len; $i++) {
            $j = $i-1;
            //check if the starting letter is # or not
            if($word[$i] == '#') {
                //insert tagname
                if($istag) {

                    //insert $tag_name
                    $this->save_tag($tag_name, $pid);
                    $istag = false;
                    $tag_name = '';
                }
                //check for comment
                if($i >= 1 && $word[$j]=='#') {
                    $this->store_cmnt .= $word[$i];
                }else{
                    //append to the store_coment if the i is 1 or -1 or $word[j]!=#
                    $this->store_cmnt .= $word[$i];//23,#
                }

            }else if($word[$i]=='@') {
                //insert tagname
                if($istag) {
                    //insert $tag_name
                    $this->save_mention($tag_name, $pid, $fbCmntInfo);
                    $istag      =   false;
                    $tag_name   =   '';
                }

                //check for comment
                if($i >= 1 && $word[$j]=='@') {
                    $this->store_cmnt   .=  $word[$i];
                }else{
                    $this->store_cmnt   .=  $word[$i];//23,#
                }
            }else if( $this->alphas($word[$i]) && $i!=0){

                if($tag_name=='') {
                    //check the length of the string 
                    $strln=strlen($this->store_cmnt);//4
                    if($strln != 0) {
                        $c  =   substr($this->store_cmnt, $strln-1, $strln);//#
                        if($c=='#' || $c=='@') {
                            $this->store_cmnt = substr($this->store_cmnt, 0, $strln-1);//23,
                            $tag_name   =   $c;
                        }
                    }
                    //$tag_name='';
                }

                //check that previous is # or @ other wise it is
                if($c=='#' || $c=='@') {
                    $tag_name .= $word[$i];

                    $istag = true;
                    //check if lenis == i then add anchor tag her
                    if($i == $len-1) {
                        $istag =false;
                        //check if it is @ or #
                        if($c=='#')
                            $this->save_tag($tag_name,$pid);
                        else
                            $this->save_mention($tag_name,$pid,$fbCmntInfo);
                        //$this->store_cmnt .= '<a >'. $tag_name.'</a>';
                    }
                }else{
                    $this->store_cmnt .= $word[$i];
                }
            }else{
                if($istag) {
                    //insert $tag_name
                    $this->save_tag($tag_name,$pid);
                    $istag = false;
                    $tag_name = '';
                }
                $this->store_cmnt .= $word[$i];
            }
        }
        $this->store_cmnt .=" ";
    }
}
  • 写回答

2条回答 默认 最新

  • doupao1530 2013-02-19 11:23
    关注

    Try This it may be help full

    function getResultStr($data, $param1, $param2){
    
        return $param1 != $param2?('<a href="'.($data[0] == '@'?"at":"hash").'">'.$data.'</a>'):$data;
    }
    
    
    function parseWord($word, $symbols){
    
        $result =   $word;
        $status =   FALSE;
    
        foreach($symbols as $symbol){
            if(($pos = strpos($word, $symbol)) !== FALSE){
                    $status =   TRUE;
                    break;
            }
        }
    
        if($status){
    
            $temp   =   $symFlag    =   '';
    
            $result =   '';
    
            foreach(str_split($word) as $char){
    
                //Checking whether chars are symbols(@,#)
                if(in_array($char, $symbols)){
                    if($symFlag != ''){
                        $result .=  getResultStr($temp, $symFlag, $temp);
                    }
                    $symFlag    =   $temp   =   $char;
    
                } else if(ctype_alnum($char) or $char == '_'){
                    //accepts[0-9][A-Z][a-z] and unserscore (_)
    
                    //Checking whether Symbol already started   
                    if($symFlag != ''){
                        $temp   .=  $char;
                    } else {
                        //Just appending the char to $result
                        $result .=  $char;
                    }
    
                } else {
                    //accepts all special symbols excepts @,# and _
                    if($symFlag != ''){
                        $result .=  getResultStr($temp, $symFlag, $temp);
                        $temp   =   $symFlag    =   '';
                    }
                    $result .=  $char;  
                }
            }
            $result .=  getResultStr($temp, $symFlag, '');
        }
        return $result;
    }
    
    function parseComment($comment){
    
        $str        =   '';
        $symbols    =   array('#', '@');
    
    
        foreach(explode(' ', $comment) as $word){
            $result =   parseWord($word, $symbols);
            $str    .=  $result.' ';
        }
    
        return $str;
    }
    
    $str = "@Richard, #McClintock, a Latin professor at $%@Hampden_Sydney @College-in #Virginia, looked up one of the @##@#@more obscure Latin words, @@##@#@%%#%@#consectetur, from a Lorem Ipsum passage, and #going@through the cites of the word in classical literature";
    
    echo "<br />Before Parsing : <br />".$str;
    echo "<br /><br />After Parsing : <br />".parseComment($str);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题