Discussion:
Anyone have a tool/script to convert <? to <?php (but not <?=)
Daevid Vincent
2014-02-12 19:43:41 UTC
Permalink
Subject pretty much says it all.

I have a lot of inherited old code where the developer used <? Instead of
<?php everywhere. Wondering if someone has a regex or script that can go
through these files and clean them up, of course it has to be aware of <?=
as valid which is what makes this tricky otherwise it's a pretty simple
search/replace.

It seems this would be a pretty useful feature to have in ZendStudio since
it is already parsing the whole file anyways and clearly colorizes the <?
<?php and <?= tags so it knows about them. I asked them once for this and of
course was met with resistance. They're too focused on making Zend Framework
than an IDE with features that would actually be beneficial to a broader
audience, you know like actual code folding for if/then/else, switch,
do/while, etc. or using array elements as objects with autocomplete, or
giving us automatic magic __get and __set if defined in an inherited class
instead of having to kludge them with @method PHPDoc tricks. But I digress.
Aziz Saleh
2014-02-12 19:57:42 UTC
Permalink
What OS are you using?

This works on Ubuntu:

find ./ -type f | xargs sed -i 's/<? /<?php /'

Aziz
Post by Daevid Vincent
Subject pretty much says it all.
I have a lot of inherited old code where the developer used <? Instead of
<?php everywhere. Wondering if someone has a regex or script that can go
through these files and clean them up, of course it has to be aware of <?=
as valid which is what makes this tricky otherwise it's a pretty simple
search/replace.
It seems this would be a pretty useful feature to have in ZendStudio since
it is already parsing the whole file anyways and clearly colorizes the <?
<?php and <?= tags so it knows about them. I asked them once for this and of
course was met with resistance. They're too focused on making Zend Framework
than an IDE with features that would actually be beneficial to a broader
audience, you know like actual code folding for if/then/else, switch,
do/while, etc. or using array elements as objects with autocomplete, or
giving us automatic magic __get and __set if defined in an inherited class
Bálint Horváth
2014-02-12 20:02:05 UTC
Permalink
NetBeans find & replace works fine also in a whole project...



*--Bálint Horváth*
*+36709468753*
Post by Aziz Saleh
What OS are you using?
find ./ -type f | xargs sed -i 's/<? /<?php /'
Aziz
Post by Daevid Vincent
Subject pretty much says it all.
I have a lot of inherited old code where the developer used <? Instead of
<?php everywhere. Wondering if someone has a regex or script that can go
through these files and clean them up, of course it has to be aware of
<?=
Post by Daevid Vincent
as valid which is what makes this tricky otherwise it's a pretty simple
search/replace.
It seems this would be a pretty useful feature to have in ZendStudio
since
Post by Daevid Vincent
it is already parsing the whole file anyways and clearly colorizes the <?
<?php and <?= tags so it knows about them. I asked them once for this and of
course was met with resistance. They're too focused on making Zend Framework
than an IDE with features that would actually be beneficial to a broader
audience, you know like actual code folding for if/then/else, switch,
do/while, etc. or using array elements as objects with autocomplete, or
giving us automatic magic __get and __set if defined in an inherited
class
digress.
Daevid Vincent
2014-02-12 22:56:50 UTC
Permalink
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
Joshua Kehn
2014-02-12 23:06:39 UTC
Permalink
<\?\s+

Will match <? but not <?=.

--jk
Post by Daevid Vincent
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.
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joshua Kehn
2014-02-12 23:10:26 UTC
Permalink
For bonus points:

(<\?\s+)\b

Capture group also looking for a word boundary.
http://l.kehn.io/image/1N202N3s082t

<\?=.+(;\s+)\?>

Matches: http://l.kehn.io/image/242l003n3q1O

Short tags? Really? Why do I bother.

--jk
Post by Daevid Vincent
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.
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
Lester Caine
2014-02-12 23:19:08 UTC
Permalink
Post by Daevid Vincent
The trick is that "<?=" is valid and legal and I want to keep those. I only want to change if they are specifically "<?"
Match to "<? " - there always has to be a space after the ? on the version you
want to convert ;)
--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daevid Vincent
2014-02-14 18:34:22 UTC
Permalink
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-14 18:53:44 UTC
Permalink
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joshua Kehn
2014-02-14 19:20:59 UTC
Permalink
+1...

You could not be so picky about inherited code and just change them when
you come across them.

--jk
Post by Aziz Saleh
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daevid Vincent
2014-02-14 19:21:58 UTC
Permalink
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
Aziz Saleh
2014-02-14 19:30:01 UTC
Permalink
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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
Post by Daevid Vincent
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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] Anyone have a tool/script to convert <? to <?php
(but
Post by Daevid Vincent
not
<?=)
Thanks guys for the replies so far, however, if it were a simple
search
Post by Daevid Vincent
and
replace I wouldn't have to ask ;-)
The trick is that "<?=" is valid and legal and I want to keep those.
I
Post by Daevid Vincent
only
want to change if they are specifically "<?"
Maybe there is some regex guru out there that knows the magic
incantation.
Hello <?= $username; ?>
Note the end semicolon on the variable. I'd want to strip all those
off
Post by Daevid Vincent
too,
but that is also not a trivial task if you think about it as it can
only
Post by Daevid Vincent
be
removed 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
I agree with the list you posted. However, if you try:

<? =$test;?>
or
<? php echo $test;?>

Both will parse out.

Which brings me back to the original regex:

'<? ' -> will match:

'<? ...'
and not any of the cases you posted, including:

'<?php'
or
'<?='

Then again, I didn't have my morning tea and could just be thinking of
something different!
Joshua Kehn
2014-02-14 19:30:36 UTC
Permalink
Use a lookahead:

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

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

--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
Nickolas Whiting
2014-02-14 19:03:36 UTC
Permalink
Here is a regex that might do the trick ... I did not test this on a large
scale but it matches any "<?" that is not followed by php (<?php), or an
equal sign ignoring any whitespace (<?=$var?>, <? =$var;?>, <? =
$var?>) .... etc ...


http://regex101.com/r/bL0wO9
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Nickolas Whiting
Consultant
Aziz Saleh
2014-02-14 19:12:00 UTC
Permalink
Post by Nickolas Whiting
Here is a regex that might do the trick ... I did not test this on a large
scale but it matches any "<?" that is not followed by php (<?php), or an
equal sign ignoring any whitespace (<?=$var?>, <? =$var;?>, <? =
$var?>) .... etc ...
http://regex101.com/r/bL0wO9
Post by Daevid Vincent
*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
Post by Daevid Vincent
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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] Anyone have a tool/script to convert <? to <?php
(but
Post by Daevid Vincent
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.
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
Post by Daevid Vincent
be
removed 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
--
Nickolas Whiting
Consultant
If your code is working, then you shouldn't worry about spaces since any
space after ? followed by = or php will produce a parser error.
Tommy Pham
2014-02-13 01:17:20 UTC
Permalink
Sent from my iPhone
Post by Daevid Vincent
Subject pretty much says it all.
I have a lot of inherited old code where the developer used <? Instead of
<?php everywhere. Wondering if someone has a regex or script that can go
through these files and clean them up, of course it has to be aware of <?=
as valid which is what makes this tricky otherwise it's a pretty simple
search/replace.
It seems this would be a pretty useful feature to have in ZendStudio since
it is already parsing the whole file anyways and clearly colorizes the <?
<?php and <?= tags so it knows about them. I asked them once for this and of
course was met with resistance. They're too focused on making Zend Framework
than an IDE with features that would actually be beneficial to a broader
audience, you know like actual code folding for if/then/else, switch,
do/while, etc. or using array elements as objects with autocomplete, or
giving us automatic magic __get and __set if defined in an inherited class
Why is it hard for search and replace in files? Normal search for '<? ' and replace with '<?php '
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daevid Vincent
2014-02-14 19:41:46 UTC
Permalink
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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nickolas Whiting
2014-02-14 19:57:01 UTC
Permalink
Any luck with the regex I supplied?

(<\?)(?!php|=|>|(?:\s+(?=\=)))

/(<\?)(?!php|=|>|(?:\s+(?=\=)))/

-
1st Capturing group (<\?)
- < matches the character < literally
- \? matches the character ? literally
-
(?!php|=|>|(?:\s+(?=\=))) Negative Lookahead - Assert that it is
impossible to match the regex below
-
1st Alternative: php
- php matches the characters php literally (case sensitive)
-
2nd Alternative: =
- = matches the character = literally
-
3rd Alternative: >
- > matches the character > literally
-
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
Post by Daevid Vincent
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';?>
<?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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Nickolas Whiting
Consultant
Daevid Vincent
2014-02-14 20:05:14 UTC
Permalink
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:***@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 = 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.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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Nickolas Whiting
Consultant
Joshua Kehn
2014-02-14 20:16:46 UTC
Permalink
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
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?
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 = 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
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';?>
<?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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Nickolas Whiting
Consultant
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daevid Vincent
2014-02-14 22:55:39 UTC
Permalink
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-----
Sent: Friday, February 14, 2014 12:17 PM
To: Daevid Vincent
Cc: PHP General list
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=)
Match: <\?(?!(=|php\s))(\n|\s+)?
Replace: <?php
$result = preg_replace("/<\\?(?!(=|php\\s))(\\n|\\s+)?/um", "<?php ",
$searchText);
http://l.kehn.io/image/3K1U3w1z0Z2a
--jk
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?
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 = 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
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';?>
<?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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Nickolas Whiting
Consultant
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joshua Kehn
2014-02-15 01:00:05 UTC
Permalink
Multiple regular expressions would be the way to go then.

Do the first one I sent (changing to <?phpcommand) then run another
matching something like <?php\w and replace with <?php $1<-- that's a
space

--jk
Post by Daevid Vincent
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)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Peter West
2014-02-15 02:45:44 UTC
Permalink
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?"
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 12:17 PM
To: Daevid Vincent
Cc: PHP General list
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=)
Match: <\?(?!(=|php\s))(\n|\s+)?
Replace: <?php
$result = preg_replace("/<\\?(?!(=|php\\s))(\\n|\\s+)?/um", "<?php ",
$searchText);
http://l.kehn.io/image/3K1U3w1z0Z2a
--jk
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?
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 = 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
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';?>
<?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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
Daevid Vincent
2014-02-20 19:34:36 UTC
Permalink
I've started writing the tool (which I will post when finished for others to use).

However, I have a few questions about preg_replace():

How do I search for '$' literal? I thought it was just backslash escape it but that isn't matching (as indicated by the semi-colon in the output)

php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>

For fun I tried without the \$ and just $ but same thing.

php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>

Taking off the $ check all together I get the match, but then how can I apply trim() to the match? The documentation seems to imply this should work no? http://php.net/preg_replace

php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>

For sanity I tried a few other things and it doesn't appear that the chosen function is processing the match variable $1?

php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>

php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>

php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>

echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5("$1").' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>

Notice how the MD5 didn't change, indicating that it's MD5'ing the literal string '$1' and verified by this

php > echo md5('$1')."\n";
06d3730efc83058f497d3d44f2f364e3

*sigh*

Ultimately I was able to craft a regex that I think will work in this instance

php > echo preg_replace("/<\?=\s*(.+?);?\s*\?/", "<?= $1 ?", 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>

But I'd like to know the answers to the two questions above for future reference
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-02-20 22:05:03 UTC
Permalink
Post by Daevid Vincent
I've started writing the tool (which I will post when finished for others to use).
How do I search for '$' literal? I thought it was just backslash escape it but that isn't matching (as indicated by the semi-colon in the output)
php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
For fun I tried without the \$ and just $ but same thing.
php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
Taking off the $ check all together I get the match, but then how can I apply trim() to the match? The documentation seems to imply this should work no? http://php.net/preg_replace
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
For sanity I tried a few other things and it doesn't appear that the chosen function is processing the match variable $1?
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5("$1").' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
Notice how the MD5 didn't change, indicating that it's MD5'ing the literal string '$1' and verified by this
php > echo md5('$1')."\n";
06d3730efc83058f497d3d44f2f364e3
*sigh*
Ultimately I was able to craft a regex that I think will work in this instance
php > echo preg_replace("/<\?=\s*(.+?);?\s*\?/", "<?= $1 ?", 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
But I'd like to know the answers to the two questions above for future reference
Try this. It works for me. If the examples don't cover all your test cases,
please include them when you respond.

[***@jim ~]$ cat junk.php
<?php

$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?=$username; ?>';
$lines[] = 'Username: <?= $username;?>';
$lines[] = 'Username: <?=$username;?>';

$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?=$username ?>';
$lines[] = 'Username: <?= $username?>';
$lines[] = 'Username: <?=$username?>';


foreach ( $lines AS $line ) {
echo preg_replace(
'/<\?=\s*(\$[\w\d_]+);?\s*\?>/',
'<?= '.trim('$1').' ?>',
$line)."\n";
}



[***@jim ~]$ php -f junk.php
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-02-20 22:10:07 UTC
Permalink
Post by Jim Lucas
Post by Daevid Vincent
I've started writing the tool (which I will post when finished for others to use).
How do I search for '$' literal? I thought it was just backslash escape it
but that isn't matching (as indicated by the semi-colon in the output)
also, the thing to watch out for when trying to match a $ in your regex, if
you use double quotes, you need to double escape the $. If you use single
quotes, you only need 1 backslash. So...

"/\\\$/" or '/\$/' would be the same
Post by Jim Lucas
Post by Daevid Vincent
php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
For fun I tried without the \$ and just $ but same thing.
php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
Taking off the $ check all together I get the match, but then how can I
apply trim() to the match? The documentation seems to imply this should work
no? http://php.net/preg_replace
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
For sanity I tried a few other things and it doesn't appear that the chosen
function is processing the match variable $1?
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
<?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
Notice how the MD5 didn't change, indicating that it's MD5'ing the literal
string '$1' and verified by this
php > echo md5('$1')."\n";
06d3730efc83058f497d3d44f2f364e3
*sigh*
Ultimately I was able to craft a regex that I think will work in this instance
<?= $username; ?>')."\n";
Username: <?= $username ?>
But I'd like to know the answers to the two questions above for future reference
Try this. It works for me. If the examples don't cover all your test cases,
please include them when you respond.
<?php
$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?=$username; ?>';
$lines[] = 'Username: <?= $username;?>';
$lines[] = 'Username: <?=$username;?>';
$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?=$username ?>';
$lines[] = 'Username: <?= $username?>';
$lines[] = 'Username: <?=$username?>';
foreach ( $lines AS $line ) {
echo preg_replace(
'/<\?=\s*(\$[\w\d_]+);?\s*\?>/',
'<?= '.trim('$1').' ?>',
$line)."\n";
}
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daevid Vincent
2014-02-20 22:59:13 UTC
Permalink
-----Original Message-----
Sent: Thursday, February 20, 2014 2:10 PM
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=)
Post by Jim Lucas
Post by Daevid Vincent
I've started writing the tool (which I will post when finished for others
to
Post by Jim Lucas
Post by Daevid Vincent
use).
How do I search for '$' literal? I thought it was just backslash escape
it
Post by Jim Lucas
Post by Daevid Vincent
but that isn't matching (as indicated by the semi-colon in the output)
also, the thing to watch out for when trying to match a $ in your regex, if
you use double quotes, you need to double escape the $. If you use single
quotes, you only need 1 backslash. So...
"/\\\$/" or '/\$/' would be the same
Post by Jim Lucas
Post by Daevid Vincent
php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
For fun I tried without the \$ and just $ but same thing.
php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
Taking off the $ check all together I get the match, but then how can I
apply trim() to the match? The documentation seems to imply this should
work
Post by Jim Lucas
Post by Daevid Vincent
no? http://php.net/preg_replace
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
For sanity I tried a few other things and it doesn't appear that the
chosen
Post by Jim Lucas
Post by Daevid Vincent
function is processing the match variable $1?
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
'Username: <?= $username; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
<?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
Notice how the MD5 didn't change, indicating that it's MD5'ing the
literal
Post by Jim Lucas
Post by Daevid Vincent
string '$1' and verified by this
php > echo md5('$1')."\n";
06d3730efc83058f497d3d44f2f364e3
*sigh*
Ultimately I was able to craft a regex that I think will work in this
instance
Post by Jim Lucas
Post by Daevid Vincent
<?= $username; ?>')."\n";
Username: <?= $username ?>
But I'd like to know the answers to the two questions above for future reference
Try this. It works for me. If the examples don't cover all your test
cases,
Post by Jim Lucas
please include them when you respond.
<?php
$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?= $username; ?>';
$lines[] = 'Username: <?=$username; ?>';
$lines[] = 'Username: <?= $username;?>';
$lines[] = 'Username: <?=$username;?>';
$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?= $username ?>';
$lines[] = 'Username: <?=$username ?>';
$lines[] = 'Username: <?= $username?>';
$lines[] = 'Username: <?=$username?>';
foreach ( $lines AS $line ) {
echo preg_replace(
'/<\?=\s*(\$[\w\d_]+);?\s*\?>/',
'<?= '.trim('$1').' ?>',
$line)."\n";
}
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Username: <?= $username ?>
Thanks for the help Jim. I modified it a bit to handle more valid variable cases...

$line = preg_replace('/<\?=\s*(\$[\w\d_\'\"\[\]\$]+);?\s*\?>/', '<?= $1 ?>', $line);

The trim() is still not working right. You can change it to MD5() and see that the value doesn't change.
While not needed in this particular case/regex, I'm curious why or more importantly how to process the matched part?

-------------------------- 8< ----------------------
<?
if ($foo_bar['test'])
?>

bar = <?=$foo;?>
bar = <?= $foo; ?>
bar = <?=$foo?>
bar = <?= $foo ?>

<?= $foo_bar['test'] ?>
<?=$foo_bar['test'];?>
<?= $foo_bar["test"] ?>
<?=$foo_bar["test"];?>
<?= $foo_bar[0] ?>
<?=$foo[$bar];?>

<?php define('DEFINED_CONSTANT','SOME DEFINITION HERE'); ?>
<?= DEFINED_CONSTANT ?>
<?= UNDEFINED_CONSTANT ?>

<? if($foo_bar['test']) ?>
<?php $foo_bar['test']='bar'; ?>
Username: <?= $foo_bar['username']; ?>
Password: <?= $foo_bar['password'] ?>
<?=$foo_bar['test']?>
<?echo 'i told you so';?>

-------------------------- >8 ----------------------

Results in :

<?
if ($foo_bar['test'])
?>

bar = <?= $foo ?>
bar = <?= $foo ?>
bar = <?= $foo ?>
bar = <?= $foo ?>

<?= $foo_bar['test'] ?>
<?= $foo_bar['test'] ?>
<?= $foo_bar["test"] ?>
<?= $foo_bar["test"] ?>
<?= $foo_bar[0] ?>
<?= $foo[$bar] ?>

<?php define('DEFINED_CONSTANT','SOME DEFINITION HERE'); ?>
<?= DEFINED_CONSTANT ?>
<?= UNDEFINED_CONSTANT ?>

<? if($foo_bar['test']) ?>
<?php $foo_bar['test']='bar'; ?>
Username: <?= $foo_bar['username'] ?>
Password: <?= $foo_bar['password'] ?>
<?= $foo_bar['test'] ?>
<?echo 'i told you so';?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Maciek Sokolewicz
2014-02-24 10:49:22 UTC
Permalink
Post by Daevid Vincent
The trim() is still not working right. You can change it to MD5() and see that the value doesn't change.
While not needed in this particular case/regex, I'm curious why or more importantly how to process the matched part?
This simply has to do with the fact that you're trim()'ing the string
before it's processed as an argument to preg_replace.

Let's pick apart the string:
preg_replace(
"/<\?=(.+?);?\s*\?/",
'<?= ' . md5(
"$1"
) . ' ?',
'Username: <?= $foo; ?>'
) . "\n";

This could also be rewritten as:
$m = md5("$1");
preg_replace("/<\?=(.+?);?\s*\?/", "<?=$m ?", 'Username:
<?= $foo; ?>')."\n";

Or even clearer:
---
$pattern = "/<\?=(.+?);?\s*\?/";
$m = md5("$1"); // note that you're running md5 here
$replace = '<?=' . $m .' ?';
$subject = 'Username: <?= $foo; ?>';

$result = preg_replace($pattern, $replace, $subject); // and only
getting to the preg_replace here. AFTER the md5 has ran

$return = $result . "\n";
---
As you can see, all that's hapenning is that you're running md5("$1")
and inserting the literal output of that string ($1 does not get
replaced with anything since preg_replace hasn't ran yet). Which in turn
means that all you get is the exact same output, for all your possible
inputs.

What you're looking for (if you want to run a callback-function) is the
preg_replace_callback. Alternatively (and potentially VERY dangerous if
you don't have FULL control over your input) you could use the /e
modifier to eval the result, and have that "changed" code do whatever it
is you want.

Personally though, I'd go for the callback-route.

- Tul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Peter West
2014-02-20 23:42:53 UTC
Permalink
What happens in php if you put the $ in a character class; e.g. [$] ?

Peter West

And he said to them, "Do you not yet understand?"
Post by Daevid Vincent
I've started writing the tool (which I will post when finished for others to use).
How do I search for '$' literal? I thought it was just backslash escape it but that isn't matching (as indicated by the semi-colon in the output)
php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
For fun I tried without the \$ and just $ but same thing.
php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>', 'Username: <?= $username; ?>')."\n";
Username: <?= $username; ?>
Taking off the $ check all together I get the match, but then how can I apply trim() to the match? The documentation seems to imply this should work no? http://php.net/preg_replace
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
For sanity I tried a few other things and it doesn't appear that the chosen function is processing the match variable $1?
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $username; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5("$1").' ?', 'Username: <?= $foo; ?>')."\n";
Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
Notice how the MD5 didn't change, indicating that it's MD5'ing the literal string '$1' and verified by this
php > echo md5('$1')."\n";
06d3730efc83058f497d3d44f2f364e3
*sigh*
Ultimately I was able to craft a regex that I think will work in this instance
php > echo preg_replace("/<\?=\s*(.+?);?\s*\?/", "<?= $1 ?", 'Username: <?= $username; ?>')."\n";
Username: <?= $username ?>
But I'd like to know the answers to the two questions above for future reference
--
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
Joshua Kehn
2014-02-14 20:11:43 UTC
Permalink
Match regular expression: <\?(?!(=|php))

Replacement (lose quotes): "<?php "

PHP code:

$result = preg_replace("/<\\?(?!(=|php))/um", "<?php ", $searchText);

http://l.kehn.io/image/2S02300S2g0W

--jk
Post by Daevid Vincent
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';?>
<?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-----
Sent: Friday, February 14, 2014 11:31 AM
To: Daevid Vincent
Subject: Re: [PHP] Anyone have a tool/script to convert <? to <?php (but
not
<?=)
<\?(?!(=|php))
http://l.kehn.io/image/1H2J0p0T472k
--jk
Post by Daevid Vincent
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-----
Sent: Friday, February 14, 2014 10:54 AM
To: Daevid Vincent
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.
Post by Daevid Vincent
*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-----
Sent: Wednesday, February 12, 2014 2:57 PM
Subject: RE: [PHP] 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.
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
--
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
--
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
Lester Caine
2014-02-14 20:38:37 UTC
Permalink
Post by Daevid Vincent
<?echo 'i told you so';?>
<?phpecho 'this one fails';?>
I've had a lot of legacy code to convert.
Since I had no plans to remove the '<?= ' ... I always have the space after
that! ... then '<? echo' got conveyed to '<?= ' If you have '<?echo' then that
can be converted as well so '<?phpecho ' also becomes '<?= ' ?

I've been using eclipse with phpeclipse for a long time now, and global search
and replace is one of the best facilities it provides. I can search, establish
how many occurrences of a string appear, and then decide how to convert that
string. When you only have half a dozen results it's often faster simply to edit
each thahn work out what to put in the global replace :)
--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...