텍스트로 언급 된 이름을 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;
}
var_dump '$ 멘션'변수? –
원하는 출력을 게시 할 수 있습니까? – Terminus
@Terminus, 원하는 출력은 단순히'Hello, @Angelina. 나는 @ Bob_Marley' (at) 언급 문자열이 출력에서 하이퍼 링크되어 있습니다. – Terungwa