Re: Anyone have a tool/script to convert <? to <?php (but not <?=)
| From: | Joshua Kehn | Date: | Fri, 14 Feb 2014 20:16:46 +0000 |
| Subject: | Re: Anyone have a tool/script to convert <? to <?php (but not <?=) | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322878@lists.php.net to get a copy of this message | ||
If you're OK with reformatting from newlines:
Match: <\?(?!(=|php\s))(\n|\s+)?
Replace: <?php
PHP code:
$result = preg_replace("/<\\?(?!(=|php\\s))(\\n|\\s+)?/um", "<?php ", $searchText);
http://l.kehn.io/image/3K1U3w1z0Z2a
--jk
On 14 Feb 2014, at 12:05, Daevid Vincent wrote:
That one matches great too! Now it’s the replacement part that’s the sticky one. I could always substitute with “<?php “ (with space) but that’s ugly. I only need to add a space in the case of <?command Also what tool are you using there? From: Nickolas Whiting [mailto:prggmr@gmail.com] Sent: Friday, February 14, 2014 11:57 AM To: Daevid Vincent Cc: PHP General list Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=) Any luck with the regex I supplied? (<\?)(?!php|=|>|(?:\s+(?=\=))) /(<\?)(?!php|=|>|(?:\s+(?=\=)))/· 1st Capturing group (<\?) o < matches the character < literally o \? matches the character ? literally · (?!php|=|>|(?:\s+(?=\=))) Negative Lookahead - Assert that it is impossible to match the regex below o 1st Alternative: php§ php matches the characters php literally (case sensitive)o 2nd Alternative: =§ = matches the character = literallyo 3rd Alternative: >§ > matches the character > literallyo 4th Alternative: (?:\s+(?=\=))§ (?:\s+(?=\=)) Non-capturing group § \s+ match any white space character [\r\n\t\f ] § Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy] § (?=\=) Positive Lookahead - Assert that the regex below can be matched § \= matches the character = literally On Fri, Feb 14, 2014 at 2:41 PM, Daevid Vincent <daevid@daevid.com> wrote: Now we're getting somewhere maybe? ;-) On my text examples it seems to look good but I think it's not much different than using a \s <? if ($foo) ?> <?= $foo ?> <?=$foo;?> <? if($foo) ?> <?php $foo='bar'; ?> <?= $foo ?> <?=$foo?> <?echo 'i told you so';?> <?phpecho 'this one fails';?> If I substitute '<?php' then I end up with this: <?php if ($foo) ?> <?= $foo ?> <?=$foo;?> <?php if($foo) ?> <?php $foo='bar'; ?> <?= $foo ?> <?=$foo?> <?phpecho 'i told you so';?> <-- this one breaks now though <?phpecho 'this one fails';?> <-- don't care as it was broken to begin with Is there a way to tweak the substitution with that same lookahead idea so if it was <? Followed by a character NOT a = then it adds a space?-----Original Message----- From: Joshua Kehn [mailto:josh.kehn@gmail.com] Sent: Friday, February 14, 2014 11:31 AM To: Daevid Vincent Cc: php-general@lists.php.net Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (butnot<?=) Use a lookahead: <\?(?!(=|php)) http://l.kehn.io/image/1H2J0p0T472k --jk On 14 Feb 2014, at 11:21, Daevid Vincent wrote:-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Nickolas Whiting ConsultantTry it... <?php $foo='bar'; ?> <?= $foo ?> <?=$foo?> <?echo 'i told you so';?> <?phpecho 'this one fails';?> The ONLY one that gives a parser error is the LAST one where yes, a space is required after a <?php That is NOT the case for <? or <?=-----Original Message----- From: Aziz Saleh [mailto:azizsaleh@gmail.com] Sent: Friday, February 14, 2014 10:54 AM To: Daevid Vincent Cc: php-general@lists.php.net Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=) What I posted (and all the others) would work, replacing all instances of "<? " (notice the space) with "<?php " should work, it will leave all existing <?= and <?php alone (since having a space after ? and before =/php would produce a parser error. Not sure why you are saying that spaces are irrelevant thou. On Fri, Feb 14, 2014 at 1:34 PM, Daevid Vincent <daevid@daevid.com> wrote:-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php*sigh* <?=$foo?> is perfectly valid so is <?=$foo;?> or any combination WITH SPACES OR NOT. If this were trivial, I wouldn't have asked the list. I've been coding PHP since 1996. ;-) So if you have <? you have to make sure it doesn't have a '=' after the '?' to convert to '<?php' Spaces are irrelevant and can NOT be relied upon as a unique feature.-----Original Message----- From: Daevid Vincent [mailto:daevid@daevid.com] Sent: Wednesday, February 12, 2014 2:57 PM To: php-general@lists.php.net Subject: RE: [PHP] Anyone have a tool/script to convert <? to <?php (butnot<?=) Thanks guys for the replies so far, however, if it were a simple searchandreplace I wouldn't have to ask ;-) The trick is that "<?=" is valid and legal and I want to keep those. Ionlywant to change if they are specifically "<?" Maybe there is some regex guru out there that knows the magicincantation.Related, for extra credit it drives me bonkers to see this: Hello <?= $username; ?> Note the end semicolon on the variable. I'd want to strip all those offtoo,but that is also not a trivial task if you think about it as it can onlyberemoved if proceeded with <?= -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php