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 변수를 한번 출력해보세요.
정확하게 원하시던 값이 들어있는게 맞는지 체크하는게 먼저일거 같아요.
https://www.php.net/manual/en/function.str-replace.php
str_replace( "#cp15#", // 찾는 문자열
"원하시는 문자열",
$a_analysis_plus_a_personality_cp15['content'] // DB에서 가져 온 부분
);
감사합니다. 도움되었습니다~!