2016-09-26 1 views
1

관리자가 등록 된 모든 회원 정보 메시지를 한꺼번에 보내고 @[email protected] 또는 @[email protected] 등을 사용하여 이름이나 이메일을 언급 할 수있는 포럼 관리 페이지를 만듭니다. 이제 메시지의 본문에 메시지의 본문이 포함되어 있거나 $_SESSION['username']을 언급하여 메시지를보고있는 현재 사용자의 사용자 이름을 입력하면 심볼의 내용이 @[email protected]으로 바뀝니다.PHP 문자열이 기호 및 특수 문자로 대체됩니다.

내가 그 일을 시도이 있었다 그것은 일이지만 하나의 메시지

첫 번째 시도에서 2 개의 다른 기호를 포함하는 경우 한 번 이메일과 이름과 같은 다른 검사 여부를 수

$match_user = str_replace("@[email protected]",$_SESSION['username'],$string);

여기 온라인으로 검색했는데 필요한 정확한 정보를 얻을 수 없었습니다. 따라서이 작업을 시도했지만 너무 많은 오류가 발생하여 누군가 나를 도울 수 있습니까? 귀하의 경우에는

두 번째 시도

<?php 
//I use this function to check if word contain in array 
function contains($string, array $array) { 
    $count = 0; 
    foreach($array as $value) { 
     if (false !== stripos($string,$value)) { 
      ++$count; 
     }; 
    } 
    return $count == count($array); 
} 
$string = Welcome @[email protected] we have sent you new info message at @[email protected]; 
$array = array('@[email protected]', '@[email protected]'); 

if(contains($string, $array)) { 
    if($array == '@[email protected]'){ 
    $match_user = str_replace("@[email protected]",$_SESSION['username'],$string); 
    }else if($array == '@[email protected]'){ 
    $match_user = str_replace("@[email protected]",$useremail,$string);  
    }else if($array == '@[email protected]'){ 
    $match_user = str_replace("@[email protected]",$userfullname,$string); 
    }else{ 
     //.... 
    } 
} 
?> 
+0

당신은 않는 str_replace에서 배열을 사용할 수 있습니다. 이 [link] (http://stackoverflow.com/questions/7605480/str-replace-for-multiple-items) –

+0

사용자 정의 함수'contains'에 대한 [array_search] (http : /php.net/manual/en/function.array-search.php) – Jeff

답변

2

당신은 문자열에 여러 대체를 사용할 수 있습니다.

예 :

$string = Welcome @[email protected] we have sent you new info message at @[email protected]; 
$array = array('@[email protected]', '@[email protected]'); 

$wordInString = array('@[email protected]','@[email protected]','@[email protected]'); 
$replaceInString = array($_SESSION['username'] ,$useremail,$userfullname); 

$match_user = str_replace($wordInString, $replaceInString, $string); 

echo $match_user; 
관련 문제