0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Simon Roberts
  • 617
  • 1
  • 7
  • 14
  • Rest assured that `strpos` does not care at all whether or not you're using HEREDOC, NOWDOC, single or double quotes to assign strings to variables. However it makes no sense to me that you talk about those while at the end you reveal you're actually using `file_get_contents` to assign the string to a variable. So most likely you're trapped somewhere and your thoughts remain confused. Here a tip: Create a new example from scratch that is self-containing and does as little as possible to demonstrate the issue. – hakre Oct 26 '14 at 14:15
  • You've lost me. Do you mean that file_get_contents is the root of the problem? Can you elaborate please? – Simon Roberts Oct 26 '14 at 14:18
  • What I wanted to say is that you're probably looking at the problem from the wrong angle. `strpos` won't cheat on you, so the problem must be somewhere else. See my answer which is a quick shot into the blue where your problem might be rooted in. – hakre Oct 26 '14 at 14:23

1 Answers1

1

The string functions you relate to (strpos, str_replace) are binary safe. That means they need to match up strings byte by byte for an exact match.

Your example shows line-breaks within the strings. Depending on platform, those can vary. It's perhaps just the case you're looking for a multiline string that is differently encoded within the string of the file you read in and the string you define inside your PHP code.

You can easily validate this by making the binary sequence of the strings you operate with visible by creating hexdumps of them:

Another sign is when using var_dump with those strings shows an unexpected different binary length.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • ok, I've checked as you suggest and the strings are indeed different when using BIN2HEX. Is there any comparison operator that will ignore line endings? – Simon Roberts Oct 26 '14 at 14:31
  • well you can do with regular expressions, for example `\R` does stand for a line-break. However you then need to quote the string to have it verbatim. A perhaps more easy way is that you normalize line-endings prior processing, so that you don't run into this comparison issue. That's most likely more appropriate in your scenario. – hakre Oct 26 '14 at 14:43