1

如何提高这段代码在 php 中的性能?
或任何替代方法来找出注释如果字符串以@then 调用at() 或字符串以"#"then 调用开头hash()

这里的示例评论是"#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 .=" ";
    }
}
4

2 回答 2

0
  • 使用 strpos 或 preg_match 或 strstr
  • 请参考 php.ini 中的字符串函数。您可以使用内置函数在一两行中完成此操作。
  • 如果它不匹配更好地编写一个正则表达式。
于 2013-02-19T05:32:59.207 回答
0

试试这个它可能是帮助充分

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);
于 2013-02-19T11:23:15.927 回答