output code AS IS
add in note
i have to paste 3 time the following code in the html body of a static webpage, instead of having to paste it and have a lot of code, i prefer to only have one line (3 times) that call the writing of the code, like this : <?php getgooglepub(); ?>
I like to make a php function that will ouput to t开发者_如何学运维he hmtl page the google analitic code or adsense.... hot to format the code to output "as is" with all the<>""''
here is the sample code to output :
<script type="text/javascript"><!--
google_ad_client = "pub-0743213818925076";
/* 728x90, date de création 11/02/10 */
google_ad_slot = "9774402576";
google_ad_width = 870;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Note, despite the creative answer, i found myselft now beeing able to make it work... for sure, my fautl.... i will copy-paste the code, for sure, you will flag the problem, since i have no clue !..
function getgooglepub()
{$google_code = <<<EOT
<script type="text/javascript"><!--
google_ad_client = "pub-0743213818925076";
/* 728x90, date de création 11/02/10 */
google_ad_slot = "9774402576";
google_ad_width = 870;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
EOT;
echo htmlspecialchars($google_code);}
after that in the html code there is the call
<?php getgooglepub(); ?>
This is the source code from the page render in firefox... abviousely not working
<script type="text/javascript"><!--
google_ad_client = "pub-0743213818925076";
/* 728x90, date de création 11/02/10 */
google_ad_slot = "9774402576";
google_ad_width = 870;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
i have read somewhere about the [data] tag ?.... it maybe a way to do that ?
If you want to output some HTML code (and see the actual HTML code without having it interpreted), you can use a combinaison of both :
<pre>
tags, so the presentation of the orinal code is kept ; i.e. linebreaks and multiple-spaces are not ignored by the browser- and a function such as
htmlspecialchars
, so<
,>
,&
and quotes are escaped -- so they aree not interpreted by the browser.
For instance :
$str = <<<HTML
<script type="text/javascript"><!--
google_ad_client = "pub-0743213818925076";
/* 728x90, date de création 11/02/10 */
google_ad_slot = "9774402576";
google_ad_width = 870;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
HTML;
echo '<pre>';
echo htmlspecialchars($str);
echo '</pre>';
die;
Shoud display the HTML code, without it being interpreted by the browser.
If you have your HTML code in a variable, use
echo htmlspecialchars($google_analytics);
See this question from yesterday for a detailed explanation.
if it's not in a variable yet, you can use HEREDOC notation for convenience:
$google_analytics = <<<EOT
<script type="text/javascript"><!--
google_ad_client = "pub-0743213818925076";
/* 728x90, date de création 11/02/10 */
google_ad_slot = "9774402576";
google_ad_width = 870;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
EOT;
echo htmlspecialchars($google_analytics);
As the answer is right, and do print code "as is" it does not do it the way i want. Maybe i dont have explain it the right way. So i have found a solution. I use a external php file with the code, and after that in the main file i use the include. that way i only have one code that i can reuse many time, thanks anyway !
精彩评论