I have a multiline string that's a mix of html and php, like this
echo '
<tr >' . '
<td colspan="3" style="border-bottom: 1px solid #C9C9C9;">' . draw_separator('pixel_trans.gif', '1', '1') . '</td>' . '
</tr>' . '
<tr >' . '
<td colspan="3" style="background: #FFFFFF;">' . draw_separator('pixel_trans.gif', '1', '10') . '</td>' . '
</tr>' . '
<tr >' . '
<td colspan="3" style="border-top: 1px solid #C9C9C9;">' . draw_separator('pixel_trans.gif', '1', '1') . '</td>' . '
</tr>' . '
<form action="' . href_link('indeed.php', 'model=' . $model . '&action=add_product') . '" method="post">' . '
<tr >' . '
<td align="right" valign="middle">' . TEXT_STEP_4 . '</td>' . '
<td align="center" valign="middle"></td>' . '
</tr>' . '
<tr >' . '
<td></td>
</tr>';
and I want to transliterate the '
with the "
, or swap the single and double quotes programmatically.
Whichever language I've tried this in, they all interpret one or more parts of the string instead of just accepting it as a character. I've tried foreach($chars as $char)
and the dreaded for($i=0;$i<strlen($str);$i )
in php, tried multiline strings and/or HEREDOC/NOWDOC in php, javascript, and perl (which just returned 106
), even Excel/VBA and regex in notepad . I have tried escaping all the escapable characters as well.
I know I could brute force it by replacing each character with some arbitrary string and then re-replacing those strings, but I was hoping for something more programmatic in case I wanted to extend VS Code to swap the characters reliably. It's more of a way for me to procrastinate than any serious need, but does anyone have any ideas?
EDIT:
All the text in the codeblock is part of the string, including the echo
statement. I suppose an eventual goal is to be able to swap the two characters in my IDE, but for now I just want to edit this block of text without going character by character (there's more of this code).
CodePudding user response:
HEREDOC would work just fine. You do need to prep your variables first, but I think this method not only makes your code more readable, but also separates logic from presentation
$pix_trans1 = draw_separator('pixel_trans.gif', '1', '1');
$pix_trans10 = draw_separator('pixel_trans.gif', '1', '10');
$herf = href_link('indeed.php', 'model=' . $model . '&action=add_product');
$tstep4 = TEXT_STEP_4 ;
echo <<<EOT
<tr >
<td colspan="3" style="border-bottom: 1px solid #C9C9C9;">$pix_trans1</td>
</tr>
<tr >
<td colspan="3" style="background: #FFFFFF;">$pix_trans10</td>
</tr>
<tr >
<td colspan="3" style="border-top: 1px solid #C9C9C9;">$pix_trans1</td>
</tr>
<form action="$href" method="post">
<tr >
<td align="right" valign="middle">$tsept4</td>
<td align="center" valign="middle"></td>
</tr>
<tr >
<td></td>
</tr>
EOT;
CodePudding user response:
In Perl, the following should work:
tr/"'/'"/
The problem is when you try to use it in a one liner and shell quoting interferes.
perl -pe 'tr/"'\''/'\''"/'
CodePudding user response:
Using the alternative syntax for control structures in PHP could be a workaround for you:
<tr >
<td colspan="3" style="border-bottom: 1px solid #C9C9C9;">
<?= draw_separator('pixel_trans.gif', '1', '1') ?>
</td>
</tr>
<tr >
<td colspan="3" style="background: #FFFFFF;">
<?= draw_separator('pixel_trans.gif', '1', '10') ?>
</td>
</tr>
<tr >
<td colspan="3" style="border-top: 1px solid #C9C9C9;">
<?= draw_separator('pixel_trans.gif', '1', '1') ?>
</td>
</tr>
<form action="<?= href_link('indeed.php', 'model=' . $model . '&action=add_product') ?>" method="post">
<tr >
<td align="right" valign="middle"><?= TEXT_STEP_4 ?></td>
<td align="center" valign="middle"></td>
</tr>
<tr >
<td></td>
</tr>
</form>
Where <?= … ?>
is the same as <?php echo … ?>
.
If you need to keep the string in a variable, you could make you of output buffering.