Re: Anyone have a tool/script to convert <? to <?php (but not <?=)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Do you have a _full_ specification of your requirements? Every time someone has offered a solution so far, you have come back with a new requirement, and those are currently scattered through the various postings.

If you start a new, but similarly named thread, with a complete list of cases, the solutions can attempt to address that, and the next person who trying to solve a similar problem will have a more compact reference.

Peter

And he asked then, "How many loaves do you have?"

On 15/02/2014 8:55 am, Daevid Vincent wrote:
No I don’t want to change

<?
    if ($foo)
?>

To

<?php if ($foo)
?>

That's ugly. And loses any block/indentations which help in readability.

What I want is to ONLY add a space in the case there is '<?command' so it becomes '<?php command' and not breaking it with '<?phpcommand'  ... adding a space all the time makes other formatting ugly then as '<? command' becomes '<?  command' with two spaces. I realize this may seem trivially cosmetic; I was just asking if that lookahead stuff can be applied to the replace portion as well somehow? Or maybe I run two separate regex matches on the parsed file to handle both cases since I intend to write a little .php script that will traverse a directory recursively to hit all the *.php files and re-write them (once this is tested of course)

-----Original Message-----
From: Joshua Kehn [mailto:josh.kehn@xxxxxxxxx]
Sent: Friday, February 14, 2014 12:17 PM
To: Daevid Vincent
Cc: PHP General list
Subject: Re:  Anyone have a tool/script to convert <? to <?php (but not
<?=)

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@xxxxxxxxx]
Sent: Friday, February 14, 2014 11:57 AM
To: Daevid Vincent
Cc: PHP General list
Subject: Re:  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 = literally
o    3rd Alternative: >
§  > matches the character > literally
o    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@xxxxxxxxxx>
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@xxxxxxxxx]
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  Anyone have a tool/script to convert <? to <?php
(but
not
<?=)

Use a lookahead:

   <\?(?!(=|php))

http://l.kehn.io/image/1H2J0p0T472k

--jk

On 14 Feb 2014, at 11:21, Daevid Vincent wrote:

Try 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@xxxxxxxxx]
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  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@xxxxxxxxxx>
wrote:

*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@xxxxxxxxxx]
Sent: Wednesday, February 12, 2014 2:57 PM
To: php-general@xxxxxxxxxxxxx
Subject: RE:  Anyone have a tool/script to convert <? to
<?php
(but
not
<?=)

Thanks guys for the replies so far, however, if it were a simple
search
and
replace I wouldn't have to ask ;-)

The trick is that "<?=" is valid and legal and I want to keep
those. I
only
want to change if they are specifically "<?"

Maybe there is some regex guru out there that knows the magic
incantation.

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
off
too,
but that is also not a trivial task if you think about it as it
can
only
be
removed if proceeded with <?=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux