tags:

views:

151

answers:

6

hi guys,

I am to edit a project of another guy who used php short tag everywhere in his script. I have short tag disabled in my server. I think converting all the short tags instead of enabling short tag in my server is the better one.

So which way to go better:

1.) Converting All short tag?

2.) Enabling short tag in my server?

3.) How do i convert all short tag to full one in batch

+1  A: 

You can try using Eclipse PDT, using it's search and replace all within a project (CTRL + H) to replace all short tags with full tags.

Extrakun
Or any other good IDE, like NetBeans.
Reinis I.
+5  A: 

Converting can be written using http://php.net/manual/en/function.token-get-all.php . Unlike find&replace, this does not affect XML declarations and so on. Converting vs. enabling ST: It depends. Many programmers probably prefers <?php to <?, so when you convert, you code will be more "standard". However, the importancy http://meta.stackoverflow.com/of "standard"-way depends on the situation.

v6ak
+1  A: 

Changing the server configuration would probably be faster. I personally wouldn't bother to search and replace, because, even though I prefer using normal tags, it's just a matter of taste. Short tags conflicting with XML declarations is not a serious issue, as you can just put the declarations inside strings.

Reinis I.
+1  A: 

For others, KoolKabin is referring to the use of <? instead of <?php as the opening tag for php code, usually used in this abbreviated form:

<?= $myvar1, $myvar2 ?>

Which is equivalent to this:

<? echo $myvar1, $myvar2 ?>

Which is itself equivalent to this, when the server is so configured with short_open_tag = on in php.ini:

<?php echo $myvar1, $myvar2 ?>

Personally, I’d convert all the short tags unless there are many pages that have nothing but short tags.

Writing some code around token_get_all() is your safest bet (as I detail in a separate answer here), but you might be able to get away with a simple sed script:

sed -e 's/<?\([ \t\r]\)/<?php\1/g' \
    -e 's/<?$/<?php/g' \
    -e 's/<?=\([ \t\r]\)\?/<?php echo \1/g'

Just beware of strings in your code (regexes, particularly) that might have the <? sequence followed by a whitespace character. See also: Useful one-line scripts for sed, where it’s pointed out that sed syntax varies, and that some don’t support the \t code (but you can type in an actual tab character).

n.b. Beware that sed doesn’t use Perl regexes and at least one of the other replies here has invalid sed syntax: e.g. \? in Perl is a literal question mark, but he intends ?, which is a literal question mark in sed. (He also replaces <?= with <?php, incorrectly omitting the “echo”.)

danorton
A: 

Definitely relpace the tags.

If your code is on a Unix/Linux/posix then something like:

find ${BASEDIR} -iname \*.php -exec rep_sht_tags.sh {} \;

where rep_sht_tags.sh contains something like:

#!/bin/bash

destdir=`dirname $1`
mkdir -p /backup/${destdir}

cp $1 /backup/$1
sed 's/<\?=/<?php /' /backup/$1 >$1

(NB not tested)

symcbean
"Definitely relpace the tags" Can you elaborate on your reasoning?
meagar
Short tags are deprecated for good reason - they are used elsewhere for other purposes
symcbean
@symcbean That's up for debate. What do you mean "used elsewhere for other purposes"?
meagar
+1  A: 

I looked a little more and I modify my earlier answer to: “definitely convert it”. It’s easy with the PHP-CLI converter below. Make sure that the system you run this on is configured with short_open_tag=on.

#!php-cli
<?php
global $argv;

$contents = file_get_contents($argv[1]) or die;
$tokens = token_get_all($contents);
$tokens[] = array(0x7E0F7E0F,"",-1);

foreach($tokens as $ix => $token) {
  if(is_array($token)) {
    list($toktype, $src) = $token;
    if ($toktype == T_OPEN_TAG) {
      if (($src == "<?") && ($tokens[$ix+1][0] != T_STRING)) {
        $src = "<?php";
        if ($tokens[$ix+1][0] != T_WHITESPACE) {
          $src .= " ";
        }
      }
    }
    else if($toktype == T_OPEN_TAG_WITH_ECHO) {
      $src = "<?php echo";
      if($tokens[$ix+1][0] != T_WHITESPACE) {
        $src .= " ";
      }
    }
    print $src;
  }
  else {
    print $token;
  }
}
danorton
On my blog I posted a more complete version with comments and an option to preserve <?= tags, changing only <? tags:http://www.danielnorton.com/2010/09/1079/converter-to-replace-php-short-open-tags-shorttags-with-long-tags
danorton