gravityboy Posted May 21, 2009 Share Posted May 21, 2009 I have a search and replace script and I want to replace actual php code on the pages it's going to search but I think the php tag and code gets excuted so it won't work. Does anyone know how to escape the tag itself? part of the code below, the part that has <A href="def">Replacement HTML code</A> I would need to be something like... <?php Replacement php code ?> =============== $search = "search text"; $replace = "replace text"; // Or uncomment this below lines if You want replace HTML codes. // "MARKER" can by any other string. It's only a marker. // This marker tells PHP where ends the string. /* $search = <<<MARKER <A href="abc">HTML code You want to search</A> MARKER; $replace = <<<MARKER ***need php here*** <A href="def">Replacement HTML code</A> MARKER; */ ================== Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/ Share on other sites More sharing options...
jsschmitt Posted May 21, 2009 Share Posted May 21, 2009 Ok... the issue is in how you have the PHP laid out... All php has to fall between the <?php and ?>... so you need to move the ?> to reflect that... Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839176 Share on other sites More sharing options...
wildteen88 Posted May 21, 2009 Share Posted May 21, 2009 Placing PHP code within strings wont get executed. However you'll need to escape variables though. Example code $source = <<<CODE <?php \$var = 'some value'; echo strtoupper(\$var); ?> CODE; // do something with $source echo $source; // outputs the code typed above To execute code within a string you'll need to use eval Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839177 Share on other sites More sharing options...
gravityboy Posted May 21, 2009 Author Share Posted May 21, 2009 I don't want it to get excuted. It is a php script that searches other php pages and replaces stuff. But I want to replace php code on the pages without the code getting run during the replace function. Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839185 Share on other sites More sharing options...
wildteen88 Posted May 21, 2009 Share Posted May 21, 2009 In that case the example code you posted should work fine. Just make sure you escape all your variables (as I demonstrated above) within the code you're search/replacing. As I said before PHP wont execute code defined within strings. Only variables. Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839193 Share on other sites More sharing options...
gravityboy Posted May 21, 2009 Author Share Posted May 21, 2009 thanks, maybe the variables was the problem, I'll test. Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839201 Share on other sites More sharing options...
Philip Posted May 21, 2009 Share Posted May 21, 2009 Or if outside the current tags and you don't want it to be executed: just use < for < and > for > Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839493 Share on other sites More sharing options...
gravityboy Posted May 22, 2009 Author Share Posted May 22, 2009 This works great but I found out by accident. If I try to search and replace something that was already on my server it doesn't work. The pages on the server were originaly text files that I added header and footer code to then changed the extention to php. These won't allow search and replace. I copied a whole directory to my c drive to make a test folder then uploaded it back by itself to the server and it works. So the question is... are the pages that were ftp'd back and forth somehow changed? Whitespace? line return? what could be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.