Replace short php tags with long php tags "<?", "<?php"

They have: 426 posts

Joined: Feb 2005

Hi have an old script of mine that for some reason uses the short opening php tag "<?" and not the long version "<?php".

I need to replace all occorances of the short tag with the long tag as it seems php 5x doesn't like the short tag.

I wrote this script but doesn't work?

<?php
function ListFiles($dir) {

    if(
$dh = opendir($dir)) {

       
$files = Array();
       
$inner_files = Array();

        while(
$file = readdir($dh)) {
            if(
$file != "." && $file != ".." && $file[0] != '.') {
                if(
is_dir($dir . "/" . $file)) {
                   
$inner_files = ListFiles($dir . "/" . $file);
                    if(
is_array($inner_files)) $files = array_merge($files, $inner_files);
                } else {
                   
array_push($files, $dir . "/" . $file);
                }
            }
        }

       
closedir($dh);
        return
$files;
    }
}

$x=1;
foreach (
ListFiles('hotscripts') as $key=>$file){
   
$fh = fopen($file, 'r+') or die("cant opem file");
   
$modify = str_replace("<?", "<?php", $file);
    if(!
$modify) $notsuccessful .=$file."</br />";
    else
$success .=$file."<br />";


if(
$success != ""){
    echo
"<b><h1>Success</h1></b>";
    echo
$success;
}
if(
$notsuccessful != ""){
    echo
"<b><h1>No Success</h1></b>";
    echo
$notsuccessful;
}
?>

If someone can give me a pointer i would be appreciative.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I'm not sure but I would try changing Array() to array():

$files = Array();
        $inner_files = Array();

becomes

$files = array();
        $inner_files = array();

I'm not sure if it is case-sensitive, but I have never seen Array()

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I'm not sure but I would try changing Array() to array():

Function names are not case sensitive.

I need to replace all occorances of the short tag with the long tag as it seems php 5x doesn't like the short tag.

I think PHP5 has this disabled by default, but you can enable it by changing this setting in your php.ini file:

short_open_tag = On

I wouldn't recommend it though because it is not encouraged to use this type of opening tag.

You got the recursion down which is where most people mess up. Smiling The biggest critical problem in your code was that you wern't opening and saving the files correctly. You opened the file for reading with $fh = fopen($file, 'r+') or die("cant opem file"); but didn't do anything with it after that. I modified that part of your code to make use of file_get_contents() and file_put_contents(). There are other comments in there where other minor changes were made.

<?php
function ListFiles($dir) {

    if(
$dh = opendir($dir)) {


       
$files = Array();
       
$inner_files = Array();

        while(
$file = readdir($dh)) {
            if(
$file != "." && $file != ".." && $file[0] != '.') {
                if(
is_dir($dir . "/" . $file)) {
                   
$inner_files = ListFiles($dir . "/" . $file);
                    if(
is_array($inner_files)) $files = array_merge($files, $inner_files);
               
/* added a conditional to only include php files */
               
} else if (substr($file, -4, 4) == '.php') {
                   
array_push($files, $dir . "/" . $file);
                }
            }
        }


       
closedir($dh);
        return
$files;
    }
}

$x=1;
/* took the function call outside the foreach loop */
$files = ListFiles('trunk');
foreach (
$files as $key=>$file){
   
/* this was the critical problem - the file was being opened, but not read;
        the actual str replacement was happening on the filename and not the file */
    #$fh = fopen($file, 'r+') or die("cant opem file");
   
$data = file_get_contents($file);
   
   
/* changed $file to $data in this next line */
   
$modify = str_replace("<?", "<?php", $data);
   
   
/* if this script is ran on a file that already uses the proper <?php tag,
        it will be changed to <?phpphp which is bad.  This next line is a
        crude way to take care of that potential problem */
   
$modify = str_replace("<?phpphp", "<?php", $modify);
   
   
/* don't forget to write the changes back to the file */
   
file_put_contents($file, $modify);
   
    if(!
$modify) $notsuccessful .=$file."</br />";
    else
$success .=$file."<br />";
}

if(
$success != ""){
    echo
"<b><h1>Success</h1></b>";
    echo
$success;
}
if(
$notsuccessful != ""){
    echo
"<b><h1>No Success</h1></b>";
    echo
$notsuccessful;
}
?>

Also, one other point I should mention. In the ListFiles() function, you are using functions to manually step through the directory. PHP5 has a function called scandir() that simply returns all the files and directories in that location.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

What about if you're using stuff like <?=$var?>? Wouldn't that script change it into

<?php
=$var
?>
(which I think is a syntax error)?

They have: 426 posts

Joined: Feb 2005

THanks, it worked.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Or you could simply open all the files in your editor and use the replace function.

replace <? with <?php
That sounds long winded, but in reality no more than a few mins to do. My editor saves the last replace I did, so it's a case of clicking replace, close and save file, replace, close and save file and so on

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

In my editor, I can open several dozen files at a time, and then do a replace on all open files.

Writing a script to do the same thing is more fun though. Smiling

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

yah, EditPlus3 does batch replacements too...

I love opening all the files in a directory and looking for a bit of text in alll of them at once - makes me feel omnipotent! Sticking out tongue

I've tried notepad++ and coffeecup and a few other editors, but this one is so familiar, like an old glove...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.