str_replace 기능 적용

str_replace 기능 적용

QA

str_replace 기능 적용

본문

str_replace 기능을 이용하여 db자료를 불러온뒤, 내용을 바꿀려고 합니다.

기능에 맞춰 아래와 같이 작성하여 "#cp1#" 과 같은 자료를 변경하고자 하는데...변경이 되지 않고, 그냥 #cp1# 로만 뿌려집니다.  이론을 배우고 이해한 것 같은데...잘 안되네요...  그리고 연속적용방법도 몰라서 같은 코드를 복사하여 여러줄로 적용중인데...  여러가지로 잘 안됩니다.

 

===============
$a_analysis_plus_a_personality = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND level='10' "); // plus_a + personality 자아 자료 불러오기     
    
$a_analysis_plus_a_personality_cp1 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp1' AND level=$abc_type_cp "); // cp1
$a_analysis_plus_a_personality_cp2 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp2' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp3 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp3' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp4 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp4' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp5 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp5' AND level=$abc_type_cp "); // cp5
$a_analysis_plus_a_personality_cp6 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp6' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp7 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp7' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp8 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp8' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp9 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp9' AND level=$abc_type_cp "); // cp9
$a_analysis_plus_a_personality_cp10 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp10' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp11 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp11' AND level=$abc_type_cp "); // cp11
$a_analysis_plus_a_personality_cp12 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp12' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp13 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp13' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp14 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp14' AND level=$abc_type_cp ");
$a_analysis_plus_a_personality_cp15 = sql_fetch(" SELECT * FROM ptest_analysis_plus where topic='243plus_a' AND subject='personality' AND link='cp15' AND level=$abc_type_cp "); // cp15
.....

$b_analysis_plus_a_personality = str_replace("#cp1#",$a_analysis_plus_a_personality_cp1['content'],$b_analysis_plus_a_personality); // cp1
$b_analysis_plus_a_personality = str_replace("#cp2#",$a_analysis_plus_a_personality_cp2['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp3#",$a_analysis_plus_a_personality_cp3['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp4#",$a_analysis_plus_a_personality_cp4['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp5#",$a_analysis_plus_a_personality_cp5['content'],$b_analysis_plus_a_personality); // cp5
$b_analysis_plus_a_personality = str_replace("#cp6#",$a_analysis_plus_a_personality_cp6['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp7#",$a_analysis_plus_a_personality_cp7['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp8#",$a_analysis_plus_a_personality_cp8['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp9#",$a_analysis_plus_a_personality_cp9['content'],$b_analysis_plus_a_personality); // cp9
$b_analysis_plus_a_personality = str_replace("#cp10#",$a_analysis_plus_a_personality_cp10['content'],$b_analysis_plus_a_personality);
$b_analysis_plus_a_personality = str_replace("#cp11#",$a_analysis_plus_a_personality_cp11['content'],$b_analysis_plus_a_personality); // cp11
$b_analysis_plus_a_personality = str_replace("#cp12#",$a_analysis_plus_a_personality_cp12['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp13#",$a_analysis_plus_a_personality_cp13['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp14#",$a_analysis_plus_a_personality_cp14['content'],$b_analysis_plus_a_personality); 
$b_analysis_plus_a_personality = str_replace("#cp15#",$a_analysis_plus_a_personality_cp15['content'],$b_analysis_plus_a_personality); // cp15
.....
===============

 

 

이 질문에 댓글 쓰기 :

답변 3

$b_analysis_plus_a_personality = str_replace("#cp1#",$a_analysis_plus_a_personality_cp1['content'],$b_analysis_plus_a_personality); // cp1

 

이 부분만 봐서 일단...

$a_analysis_plus_a_personality_cp1['content'] 변수와 $b_analysis_plus_a_personality 변수를 한번 출력해보세요.

정확하게 원하시던 값이 들어있는게 맞는지 체크하는게 먼저일거 같아요.

str_replace 기능이해가 아직도 100% 안되고 있습니다.
우선은 해결을 했는데...왜? 인지는 잘 모르겠네요.

해결(?) 은 $a_analysis_plus_a_personality_cp1['content'] 변수와 $b_analysis_plus_a_personality 변수들의 순서를 변경하였더니 str_replace 기능이 먹힙니다.

엑스엠엘님이 적으신 댓글에 보시면 링크 있는데 거기에 예제도 있어요.
str_replace("찾는 문자열", "바뀔 문자열", "원본 데이터");

거기에 있는 예제를 가지고 온건데
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
위의 것을 실행하면 $bodytag에는 <body text='black'>이라는 값이 남게 됩니다.

외우지는 않더라도 반드시 원리를 이해하고 쓰셔야해요.

답변을 작성하시기 전에 로그인 해주세요.
전체 15
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT