2011-01-18 5 views
0

매우 단순한 preg replace로 바꿀 수는 있지만, 나는 옳다고 생각하지 않습니다.preg replace 키워드를 찾으십시오.

{this_is_a_key}과 같은 키워드를 검색하고 다른 것으로 바꾸어야합니다.

$text = 'this is a sentence with a {this_is_a_key} in it'; 
echo preg_replace('/\{\w{1}\}/i','keyword',$text); 
//output should be 
//## "this is a sentence with a keyword in it" ##// 

은 지금까지 나를 위해 작동하지 않습니다와 나는 ()[]의 조합이지만

+0

가능한 복제본 [정규식으로 바꾸는 방법을 알려 주실 수 있습니까?] (http://stackoverflow.com/questions/3372735/could-you-tell-how-to-replace-by-regular-expression) – Val

답변

1

\w{1} 하나를 의미 행운 "단어 문자"를 시도했다. 대신 \w+ 또는 [^\}]+을 사용해보십시오.

또한 \w을 사용할 때 i 수정자가 필요하지 않습니다.

+0

i 분명히 메신저를 잘못 시도한 메신저를 시도했습니다 :) – Val

+0

메신저 대신에 str_replace를 사용해야하는 contstant replacers를 찾으십시오. http://stackoverflow.com/questions/3372735/could-you-tell-how-to -replace-by-regular-expression – Val

+0

mvds 꽤 좋은 답변을 주었다. 대체 배열에서''{this_is_a_key} ''를 키로 사용하십시오. 그것이 독자가 운동을하는 의미입니다. * –