2016-07-31 2 views
0

텍스트로 언급 된 이름을 links로 강조하는 PHP 클래스가 있습니다. 주어진 텍스트에서 @character를 검색하고 그 이름을 검사 할 수있는 PHP 클래스가 있습니다. 그 성품을 따라 가라.PHP 클래스의 함수에서 변수 반환하기 (작동하지 않는 리턴 만 작동합니다)

문제는 텍스트 처리를 담당하는 방법 (public function process_text ($text_txt){})이 echo 일 때 클래스의 반환 값이 인쇄되지 않는다는 것입니다. 그러나 return 언어 구조를 print 또는 echo으로 변경하면 구문 분석에 성공하고 처리 된 문자열이 출력됩니다. 내 return이 아니고 print이 아니어야 반환 문자열을 내 CMS의 설명 테이블에 저장할 수 있습니다.

친절 아래의 전체 코드를 확인하고 조언 :

class mentions { 
    public $print_content = ''; 
    private $m_names = array(); 
    private $m_denied_chars = array(
     "@", 
     "#", 
     "?", 
     "¿" 
    ); 
    private $m_link = "http://example.com/"; // + name of the username, link or whatever 

    /* 
    * this class can also be used for specific links 
    * start editing from here 
    * */ 

    public function add_name ($name) { 
     array_push($this->m_names, $name); 
    } 

    public function process_text ($text_txt) { 
     $expl_text = explode(" ", $text_txt); 

     /* 
     * a character will be ignores which can be specified next this comment 
     * :) 
     * */ 

     $sp_sign = "@"; // this is what you can change freely... 

     for ($i = 0; $i < count($expl_text); ++$i) { 
      $spec_w = $expl_text[$i]; 
      $print_link = false; 
      $name_link = ""; 
      if ($spec_w[0] == $sp_sign) { // then can be a mention... 
       $name = ""; 
       $break_b = false; 
       for ($x = 1; $x < strlen($spec_w); ++$x) { 
        if ($spec_w[$x] == '.' || $spec_w[$x] == ",") { 
         if (in_array($name, $this->m_names)) { 
          $print_link = true; 
          $name_link = $name; 
          break; 
         } 
        } 
        if (in_array($spec_w[$x], $this->m_denied_chars)) { 
         $break_b = true; 
         break; 
        } 
        $name .= $spec_w[$x]; 
       } 
       if ($break_b == true) { 
        $print_link = false; 
        break; 
       } else { 
        if (in_array($name, $this->m_names)) { 
         $print_link = true; 
         $name_link = $name; 
        } 
       } 
      } 
      if ($print_link == true) { 
       $this->print_content = "<a href=\"".$this->m_link."".$name_link."\">".$spec_w."</a>"; 
       if ($i < count($expl_text)) $this->print_content .= " "; 

      } else { 
       $this->print_content = $spec_w; 
       if ($i < count($expl_text)) $this->print_content .= " "; 
      } 
      return $this->print_content; 
     } 
    } 
} 
###### create new class object and process raw data ###### 
$mentions = new mentions; 
$raw_data = 'Hello, @Angelina. I am @Bob_Marley.'; 

$expr = '#(?:^|\W)@([\w-]+)#i'; 
preg_match_all($expr, $raw_data, $results); 
if(!empty($results[1])) { 

    foreach($results[1] as $user) { 
     $mentions->add_name($user); 
    } 
    /* 
    ------------------------------------ 
    */ 
    $commenData = $mentions->process_text($raw_data); 
    echo $commenData; 
} 
+0

var_dump '$ 멘션'변수? –

+0

원하는 출력을 게시 할 수 있습니까? – Terminus

+0

@Terminus, 원하는 출력은 단순히'Hello, @Angelina. 나는 @ Bob_Marley' (at) 언급 문자열이 출력에서 ​​하이퍼 링크되어 있습니다. – Terungwa

답변

0

답을 @Terminus에 의해. 루프 내에서 리턴이 있으면 루프 (및 전체 함수)가 인터럽트되고 리턴되는 값이 즉시 리턴됩니다. 그것이 작동하는 방법입니다. 그것을 좋은 대답으로 쓰려고했지만 그렇게 할 수 없었습니다. 결국 수업을 다시 작성하게 되었습니까? ideone.com/vaV0d2 내가 테스트 var_dump에 남았고 ideone에 의해 제공된 출력이 HTML로 표시되도록 링크 태그를 허용하지 않지만 서버에서 실행하면 올바른 것임을 유의하십시오.

관련 문제