2016-07-31 100 views
0

我有一个PHP类,突出显示文本中提到的名称链接。它可以搜索给定文本中的@character并检查名称跟随那个角色。从php类中的函数返回一个变量(返回不工作只回声作品)

问题是,当我负责处理文本的方法(public function process_text ($text_txt){})时,类的返回值没有打印出来。但是,当我将return语言结构更改为printecho时,解析是成功的,并且打印出处理的字符串。我需要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'$ mentions'变量? –

+0

你可以发布所需的输出吗? – Terminus

+0

@Terminus,期望的输出只是'你好,@Angelina。我是@ Bob_Marley'(at)提到的字符串在输出中是超链接的。 – Terungwa

回答

0

答案由@Terminus。如果你在循环中有一个返回值,循环(和整个函数)将被中断,返回的值将立即返回。这就是它的工作原理。试图写出这是一个很好的答案,但不能。最终有点改写你的课程。 ideone.com/vaV0d2请注意,我遗留在var_dump测试中,并且由ideone提供的输出不允许链接标记显示为html,但如果您从服务器运行它,它将是正确的