2017-12-04 2 views
-1

번역 할 단어를 추가하려고합니다. 그러나 str_replace는 작동하지 않습니다.php str_replace가 단어 번역을 위해 작동하지 않습니다.

언어에 대한 데이터베이스 테이블이 있습니다.

예를 들어, 사용자가이 단어를 참조하십시오 사용자가 다음 영어을 choosed 할 경우

영어 : 무스타파 그의 친구 아멧 오늘 영화관에 갔다합니다.

터키어 : :

사용자는 예를 들어, 사용자가이 단어를 참조하십시오 터키어 다음 choosed 할 경우 무스타파을 bugün arkadaşı 아멧 ILE birlikte sinemaya gitti. 이 같은

내가 사용한 않는 str_replace :

$splaceHolder = array("{{username}}", 
          "{{otherusername}}"); 

    $sname = array("<a href='".$base_url.$username."'>".$username."</a>", 
        "<a href='".$base_url.$otherusername."'>".$otherusername."</a>"); 

여기 str_replace :

$note = str_replace($splaceHolder, $sname, $lang['text'][$lang]); 

$lang['text'][$lang] 언어 테이블 및 텍스트에서 오는 영어입니다 : 들어

{{username}} went to the cinema with his friend {{otherusername}} today. 

터키어 :

{{username}} bugün arkadaşı {{otherusername}} ile birlikte sinemaya gitti. 

여기에서는 str_replace가 작동하지 않습니다. 그것은 영어에 대해이 {{username}} went to the cinema with his friend {{otherusername}} today.무스타파에 보여

그의 친구 아멧 오늘 영화관에 갔다. 이 무스타파 상점 터키어이 {{username}} bugün arkadaşı {{otherusername}} ile birlikte sinemaya gitti.위한 쇼

bugün arkadaşı 아멧 ILE birlikte sinemaya gitti.

그러나 나는 다음과 같은 방식으로 만 얻을 수 있습니다.

{{username}} 님은 {{otherusername}} 친구와 함께 영화를 보러갔습니다.

+1

잠시 전에이 질문을하지 않았습니까? https : // stackoverflow.com/questions/47629103/language-translate-grammer – Andreas

+0

@Andreas 그것은 str_replace 질문이 아닙니다. – Azzo

+1

하지만 답은입니다. 그리고 당신은 그의 대답의 "동의"를 제거했습니다. – Andreas

답변

1

예를 들어 str_replace가 작동합니다.

이 시도 :

<?php 
    $string = "{{username}} went to the cinema with his friend {{otherusername}} today."; 

    $res = str_replace("{{username}}", "firstuser", $string); 
    $res = str_replace("{{otherusername}}", "seconduser", $res); 
    echo "result: ".$res."<br />"; 
?> 

심지어 당신이 모두가 하나의 선으로 대체 수행 할 수 있습니다

$res = str_replace(["{{username}}", "{{otherusername}}"], ["firstuser", "seconduser"], $string); 

작품을 나를 위해.

+0

네, 아무 문제없이 잘 작동합니다. 그러나 그 텍스트는 테이블에서 나온 다음 그 시간에 일어나는 문제가 될 것입니다. – Azzo

+0

그런 다음 인코딩에 문제가 있어야합니다. 데이터베이스가 php가 사용하는 기본 인코딩 인 utf8이 아니며 str_replace를 사용하기 전에 데이터베이스 문자열의 인코딩을 변경해야합니다. 먼저 어떤 인코딩이 데이터베이스를 사용하고 있는지 확인한 다음 php iconv 함수를 살펴보아야합니다. http://php.net/manual/en/function.iconv.php – jeprubio

+0

gunaseelan 대답 :'str_replace '대신'preg_replace'를 사용해보십시오. '내 문제를 해결했다. 그래서 당신의 대답은 나도 그것을 시도해 좋은 것입니다. – Azzo

관련 문제