0

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"; ?>

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'; ?> 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.

13
  • 8
    why not just hello Commented Jan 5, 2017 at 1:50
  • 2
    ^ if I could upvote that 50 times, I would. But then they'd probably call me "trigger happy" and I'd be out of votes. Commented Jan 5, 2017 at 1:50
  • 1
    @Fred-ii- start off the year in PHP tag with headscratcher questions Commented Jan 5, 2017 at 1:54
  • 1
    @Ghost "start off the year in PHP" - I guess that the word "right" was appropriately left out ;-) Happy New Year btw <cheers> Commented Jan 5, 2017 at 1:55
  • 1
    When you do str_replace and replace with PHP code inside PHP tags, that PHP code does not get evaluated. You would have to use something like eval (Don't use eval). So instead you get a literal <?php ... ?> which your browser just thinks is a long HTML element because it is within angle brackets. Therefore it doesn't even show that output in your browser (unless you view source). Bottom line, just use hello as Ghost said. Commented Jan 5, 2017 at 2:00

3 Answers 3

3

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'; > 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().

Sign up to request clarification or add additional context in comments.

2 Comments

I want to see the result of <?php echo 'hello'; ?> and not the tags. I want to see the word hello
Good and thoughtful description on this answer, it also addresses both cases, should they want the literal PHP or the result.
1

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)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.