从php类中的函数返回变量(返回不工作只有echo工作)

时间:2016-07-31 11:22:41

标签: php

我有一个PHP类,它突出显示文本中提到的名称为links。它可以在给定文本中搜索@character并检查该字符后面的名称。

问题是当我echo负责处理文本的方法(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;
}

1 个答案:

答案 0 :(得分:0)

回答@Terminus。如果在循环内部返回,则循环(和整个函数)将被中断,并且将立即返回返回的值。这就是它的工作原理。试图把它写成一个好的答案,但无法做到。最后重写你的课程了。 ideone.com/vaV0d2请注意我留在测试var_dump中,并且ideone提供的输出不允许链接标记显示为html,但如果从服务器运行它,它将是正确的< / p>