Jump to content

Recommended Posts

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;

*/

==================

Link to comment
https://forums.phpfreaks.com/topic/159125-escape-the/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839177
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839193
Share on other sites

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? 

 

Link to comment
https://forums.phpfreaks.com/topic/159125-escape-the/#findComment-839686
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.