내용보기 치환할 때, 모바일과 모바일이 아닌 것 다르게 보이기

내용보기 치환할 때, 모바일과 모바일이 아닌 것 다르게 보이기

QA

내용보기 치환할 때, 모바일과 모바일이 아닌 것 다르게 보이기

본문

글작성할 때

{광고}라고 치면, '내용보기' 화면에서 광고가 출력되는데,

모바일과 모바일이 아닐 때, 그 내용이 다르게 출력되도록 하고 싶습니다.

즉, 광고가 표시되는데

 

모바일일때는 (소스1) 의 내용이 표시

모바일이 아닐때는 (소스2) 의 내용이 표시

 

지금은 두가지가 모두 표시가 되는데, 틀린 부분이 무엇일까요?

 


<?php
$content = get_view_thumbnail($view['content']);
$content = str_replace('{광고}', '
<?php if(!G5_IS_MOBILE) { ?>
<ins class="adsbygoogle"
style="display:block; margin:15px 0;"
data-ad-format="fluid"
data-ad-layout-key="-fc+5g+70-cl-1m"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="8345951427"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php } ?>
<?php if(G5_IS_MOBILE) { ?>
<ins class="adsbygoogle"
style="display:block; text-align:center; margin:15px 0;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="5305581469"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php } ?>
', $content);
echo $content;
?>

이 질문에 댓글 쓰기 :

답변 2


<?php
$content = get_view_thumbnail($view['content']);
if(!G5_IS_MOBILE) {
    $content = str_replace('{광고}', '<ins class="adsbygoogle"
style="display:block; margin:15px 0;"
data-ad-format="fluid"
data-ad-layout-key="-fc+5g+70-cl-1m"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="8345951427"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>',$content);
}else{
    $content = str_replace('{광고}', '<ins class="adsbygoogle"
style="display:block; text-align:center; margin:15px 0;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="5305581469"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>',$content);
}
echo $content;
?>

이런식으로 순서를 바꾸시면 됩니다.


<?php
$ad = '';
if(!G5_IS_MOBILE) {
$ad = <<<HEREDOC
<ins class="adsbygoogle"
style="display:block; margin:15px 0;"
data-ad-format="fluid"
data-ad-layout-key="-fc+5g+70-cl-1m"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="8345951427"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
HEREDOC;
} else {
$ad = <<<HEREDOC
<ins class="adsbygoogle"
style="display:block; text-align:center; margin:15px 0;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="5305581469"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
HEREDOC;
}
$content = str_replace('{광고}', $ad, $content);
echo $content;
?>

수정을 해주니까 제가 썼던 광고와 함께 작성한 내용은 전부 사라지고, 광고만 나와요.
제가 젤 위에 써놓은 소스로 하면은 두개가 출력되는 이유는 뭘까요?
모바일과 아닐때로 나눠서 표시되도록 쓴다고 썼는데...어떤 부분이 잘못되었을까요?

define('G5_IS_MOBILE', true);
또는
define('G5_IS_MOBILE', false);
로 바꿔가며 기존에 값이 할당된 $content 문자열에서 {광고} 부분만 정상적으로 바뀌는 것을 확인할수 있는 예제입니다.


<?php
define('G5_IS_MOBILE', false);
// define('G5_IS_MOBILE', true);
// $content = get_view_thumbnail($view['content']);
$content = 'a<hr>{광고}<hr>b';

$ad = '';
if(!G5_IS_MOBILE) {
$ad = <<<HEREDOC
<ins class="adsbygoogle"
style="display:block; margin:15px 0;"
data-ad-format="fluid"
data-ad-layout-key="-fc+5g+70-cl-1m"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="8345951427"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
HEREDOC;
} else {
$ad = <<<HEREDOC
<ins class="adsbygoogle"
style="display:block; text-align:center; margin:15px 0;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-8618685012773575"
data-ad-slot="5305581469"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
HEREDOC;
}

$content = str_replace('{광고}', $ad, $content);
echo $content;
?>


기존 코드에서 문제가 되는 곳은 비교 구문이 str_replace 함수에 인자로 넘겨지는 부분입니다.
str_replace 함수는 문자열을 조작할때 추가적인 PHP 해석을 수행하지 않으며
PHP tag 도 잘못된 형태로 구성되어 있습니다.

다음 코드는 php 가 해석되는 것이 아닌 단순 문자열 그대로 취급되고

'<?php if(!G5_IS_MOBILE) { echo "!G5_IS_MOBILE"; } else { echo "G5_IS_MOBILE"; } ?>'


해석과 실행을 기대한다면 다음처럼 시도 되어야 합니다.

<?php if(!G5_IS_MOBILE) { echo "!G5_IS_MOBILE"; } else { echo "G5_IS_MOBILE"; } ?>

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

회원로그인

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