<?php echo $form->create(); ?>
<?php echo $form->hidden('id'); ?>
<?php echo $form->input('name')); ?>
<?php echo $form->submit('Save'); ?>
<?php echo $form->end(); ?>
I want to replace "; ?" with " ?".
I used the vi command ":%s/; \?/ \?/g" to do that. I got the following output
<?php echo $form->create() ??>
<?php echo $form->hidden('id') ??>
<?php echo $form->input('name')) ??>
<?php echo $form->submit('Save') ??>
<?php echo $form->end() ??>
Actually, I need the following output.
<?php echo $form->create() ?>
<?php echo $form->hidden('id') ?>
<?php echo $form->input('name')) ?>
<?php echo $form->submit('Save') ?>
<?php echo $form->end() ?>
Can you give the explanation for this strange behavior?
The \? is the vi-regular-expression for the normal ? in other program's regular expressions.
You can look at :h regex to verify this (or more precisely :h E61).
So when using ; \? you match ; (no space) and ;<space> (one space). From these matches the greediest one (see E61 - as many as possible) will be replaced with a ? thus resulting in two question-marks (one new one and the old one) when there is a space present.
The correct expression would be: s/; ?/ ?/g
EDIT: Fixed explanation to be more precise.
:%s/; \?/ \?/g
; \? match ;<space> zero or one time, and it replaces them with ?.
So you get the original ? with another ?.
Related
The shipping and billing address dropdowns during OpenCart checkout (existing addresses, logged in user) don't show items such as company name, address line 2 and post code. In fact it appears to me that the optional/non-mandatory fields are not shown in the dropdown.
How do I fix it? Is there a setting for this?
You'll need to go to *ayment_address.tpl & shipping_address.tpl
Go to: ../public_html/catalog/view/theme/[default]/template/checkout/payment_address.tpl/shipping_address.tpl
Find & Replace:
<?php echo $address['firstname']; ?> <?php echo $address['lastname']; ?>, <?php echo $address['address_1']; ?>, <?php echo $address['city']; ?>, <?php echo $address['zone']; ?>, <?php echo $address['country']; ?>
With:
<?php echo $address['firstname']; ?> <?php echo $address['lastname']; ?>, <?php echo $address['company']; ?>, <?php echo $address['address_1']; ?>, <?php echo $address['address_2']; ?>, <?php echo $address['city']; ?>, <?php echo $address['zone']; ?>, <?php echo $address['country']; ?>, <?php echo $address['postcode']; ?>
This has worked for me in Opencart 2.0.1.1
I need to search and replace on file this:
<?php echo lang_xxxx; ?>
To this:
<?php echo $lang['lang_xxxx']; ?>
How to do this? The lang_xxxx is not unique, can exists anothers like lang_yyyy, lang_zzzz, etc...
You could use the following expression:
Live Example
(lang_[^;]*?);
and replace it with:
$lang['$1'];
or:
$lang['\1'];
Results:
Input: <?php echo lang_xxxx; ?>
Output: <?php echo $lang['lang_xxxx']; ?>
Input: <?php echo lang_es; ?>
Output: <?php echo $lang['lang_es']; ?>
I was looking to build a sed that would globally kill short tags in my scripts as there is a LOT of legacy stuff floating around that needs to be banished. I was working of a regex but it's being greedy so am looking for a non-greedy sed that will replace all occurances of
<\?=(.*)\?> ## Pattern
<?php echo $1; ?> ## Replacement
This WILL turn <?=$foo?> into <?php echo $foo; ?> but it will also turn <?=$foo?><?=$bar?> into <?php $foo?><?=$bar; ?> as well as duplicates the line of the match.
The SED looks like sed -i -e "s/<?=\(.*\)?>/<?php echo \1; ?>/g" file
My answer would assume that
<?=$foo?><?=$bar?> ## in
<?php echo $foo; ?><?php echo $bar; ?> ## out
You can try with
sed 's/<[?]=\([^?]*\)/<?php echo \1; /g' file
Could you help me with regular expressions in exec sed?
Example code:
<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>
I need to delete:
<?php echo "This code need to delete"; ?>
In all files, and keep
<? echo 'This code need to keep'; ?>
I tried to do it like this:
find ./ -type f -name \*.php -exec sed -i -r 's/<\?php.*\?>//g' {} \;
But this way doesn't work correctly. (delete all code)
Use a negated character class instead of .* because .* is greedy which matches any character as much as possible.
find ./ -type f -name \*.php -exec sed -i -r 's/<\?php[^>]*\?>//g' {} \;
You could use -name '*.php' instead of -name \*.php in the above.
Example:
$ echo '<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>' | sed -r 's/<\?php[^>]*\?>//g'
<? echo This code need to keep; ?>
Using gnu awk you can do this to get rid of first <?php...?> block:
cat file
<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>
awk -v RS='\\?>' '!/<\?php /{printf $0 RT}' file
<? echo 'This code need to keep'; ?>
I'd like to replace all of the php short tags to full blown ones.
Example source text:
<a href="<?=$baseurl?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ?>></a>
Expected final result:
<a href="<?php echo $baseurl; ?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ?>></a>
I got as far as this:
Find what: <?=(.*)?>
Replace with: <?php echo \1; ?>
But the result is:
<a href="<?php echo $baseurl?>random/<?php echo $offset+$per_page; ?>" class="arrows right" <?php echo $displayright; ; ?>></a>
So it doesn't really work when there are multiple php opening and closing tags on one line.
How do I fix this? Thanks!
Yes, because the * operator is greedy and will try to match everything up to the final ?. You can fix this by matching everything except the ? in the closing tag:
Find what: <?=([^?]*)?>
Replace with: <?php echo \1; ?>
Put this in the Search for field:
<\?=([^?]+)\?>
And this in the replacement field:
<?php echo \1; ?>
When you use *, Notepad++ matches as much as it can, so you need to tell it what you don't want to match so it stops without finding the whole line:
Find: <?=([^?]*)?>
Replace: <?php echo \1; ?>
Using * in your regex makes it "greedy" and you don't want that. You want the first occurrence of ?> to be matched and not the last. Also, the ? character is special in regexes so you need to escape it too:
<\?=([^?]+)\?>