My understanding is that using the NOWDOC functionality in PHP (v5.3 and higher), no escaping of characters is required.
I'm trying to find and replace text in a PHP file. The text is:
$OldString = <<<'EOD'
<fieldset class="foobar" id="comments">
<legend><?php echo TABLE_HEADING_COMMENTS; ?></legend>
<?php echo foobar_field('comments', '45', '3'); ?>
</fieldset>
EOD;
This text does appear in the target PHP file, but str_replace
does not find it. Neither does str_pos
(which I'm using for testing).
I wondered if this was due to the special characters in the string but each line is found if I conduct a strpos
or str_replace
for them individually, for example...
$OldString = <<<'EOD'
<?php echo zen_draw_textarea_field('comments', '45', '3'); ?>
EOD;
...works as I would expect.
It seems to be the multiple lines that are causing the problem, but the whole point of HEREDOC and NOWDOC is that they enable placing multiple lines of text into one variable.
My search and replace strings are:
$pos = strpos($content, $OldString);
if ($pos !== false) {
...
and
$content = str_replace($OldString, $NewString, $content);
If it makes any difference, I'm obtaining the text from a file using file_get_contents
.