2013-02-19 3 views
1

Как повысить производительность этого кода в php?
или любой альтернативный метод, чтобы выяснить, комментарий, если строка начинается с @ затем вызвать at() или если строка начинается с "#" затем вызвать hash()Как повысить производительность этого кода в php?

здесь образец комментарий "#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 .=" "; 
    } 
} 

ответ

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 #[email protected] the cites of the word in classical literature"; 

echo "<br />Before Parsing : <br />".$str; 
echo "<br /><br />After Parsing : <br />".parseComment($str); 
0
  • использование strpos или preg_match или strstr
  • Пожалуйста, обратитесь к строковым функциям в php. Вы можете сделать это в строке или двух с встроенными функциями.
  • Если это не соответствует правильному написанию регулярного выражения.
Смежные вопросы