I'm trying to find a block of text and replace it with a modified block of text and for some reason it's not working. I can execute the following line in my phpDesigner editor and it works perfectly, but when I try to execute the same command on my linux webserver it doesn't work.
Can someone help me out with a solution whether it's via regex or some other method?
$tmp_code = str_replace('<!--
{if="$membership.field_files"}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Files</span></a></li>
{else}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Files</span></a></li>
{/if}
-->','{if="$membership.field_files"}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Add/Remove Offers, Products and Files</span></a></li>
{else}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Add/Remove Offers, Products and Files</span></a></li>
{/if}',$tmp_code);
The problem is more than likely line endings (i.e. your machine vs *nix).
Your editor should have an option for saving files using a particular line ending. I suggest making it the same as your production environment (i.e. *nix)
Try substitution of " " for newlines in your search string. Make sure to use the double quotes as single quotes won't interpret some escape sequences for special characters.
Sources:
It probably has to do with the white space (new lines and tabs). You could:
$tmp_code = preg_replace('<!--\s*{if="$membership.field_files"}\s*<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Files</span></a></li>\s*{else}\s* <li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Files</span></a></li>\s*{/if}\s*-->','{if="$membership.field_files"}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Add/Remove Offers, Products and Files</span></a></li>
{else}
<li><a href="/Members/Listings.html?id={$listing.id}&action=files"><span>Add/Remove Offers, Products and Files</span></a></li>
{/if}',$tmp_code);
Which uses \s* to search for any amount of whitespace between lines, instead of the exact amount you type.
I have been able to get around this problem by setting the internal option setting for PCRE_DOTALL
using the "s
" in the pattern string.
Example:
$pattern = '# ...pattern goes here... #s';
Any dot (period) character inside that pattern will match anything including new lines. By default it does not include new lines.
Reference: PCRE Pattern Modifiers