简体   繁体   中英

Replacing string with PHP code

I am trying to replace a certain string with some PHP code using the str_replace function. I want to replace "[GOOGLE]" with <?php echo "hello"; ?> <?php echo "hello"; ?>

Here's what I have so far:

$text = str_replace("[GOOGLE]", "<?php echo 'hello'; ?>", $text);

When running this code, it does replace [GOOGLE] , but the replacement is not shown in the browser. When I go to see the page's source code, the replacement is: <?php echo 'hello'; ?> <?php echo 'hello'; ?> you can see the PHP tag for some reason. However, nothing is displayed on the page. How do I correct this problem so that [GOOGLE] is replaced with hello? Note: I do not want to replace my PHP code in the str_replace function with the actual "hello" string, I want to do this with PHP code. For now, I am trying to go with something simple. My goal is to replace [GOOGLE] with an if/else statement and Ad code for my CMS.

Your echo appears misplaced. It seems to me you want to output the result of replacing "[GOOGLE]" with "hello", in which case (1) perform the replacement then (2) echo the result. Perhaps:

$text = str_replace("[GOOGLE]", "hello", $text);
echo $text;

The reason you don't see it in the browser is that replacement, <?php echo 'hello'; > <?php echo 'hello'; > is within angled brackets. Angled brackets are special to HTML, in that they denote an operation the browser is to perform. For example, <b> means "start bolding text". When the browser sees "<?php ... ?>" it considers that a tag. It doesn't know what to do with the tag, so nothing appears rendered. However, it does remain in source as that text is, indeed, part of the source.

If you literaly want to see "<?php echo 'hello'; ?>" then you need to escape the angled brackets:

$text = str_replace("[GOOGLE]", "&lt;?php echo 'hello'; ?&gt;", $text);

Here the angled brackets have been replaced with their escaped versions: "<" becomes "&lt;" and ">" becomes "&gt;". Of course, you might tire of doing this manually. In which case, consider using htmlspecialchars() .

I guess you want to display php code on html page. You need to use htmlspecialchars() for that because this string will be partly interpreted as html tag.

In your example it would be:

$text = str_replace("[GOOGLE]", "<?php echo 'hello'; ?>", $text);
// later when producing html...
echo htmlspecialchars($text, ENT_QUOTES); //default encoding is UTF-8

WARNING: It's mandatory to do it with every user supplied content btw, because javascript could be also interpreted and someone could do nasty things with it (XSS)

I'm starting off with something simple for right now to learn how to do it. I want to really replace [GOOGLE] with Adsense code to use in my CMS

Don't do it this way.

Put your Adsense code in an include or a variable or something, and do:

$text = str_replace("[GOOGLE]", file_get_contents('google-analytics.html'), $text);

or:

$googleAnalytics = '<script> the code Google gives you </script>';
$text = str_replace("[GOOGLE]", $googleAnalytics, $text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM