How do i convert php short tag to full one
This question already has an answer here:
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 https://meta.stackoverflow.com/of "standard"-way depends on the situation.
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/<?([ tr])/<?php1/g'
-e 's/<?$/<?php/g'
-e 's/<?=([ tr])?/<?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).
nb Beware that sed doesn't use Perl regexes and at least one of the other replies here has invalid sed syntax: eg ?
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”.)
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;
}
}
链接地址: http://www.djcxy.com/p/59426.html
上一篇: XAMPP不识别PHP,必须替换所有的<? 与<?php
下一篇: 我如何将PHP短标签转换为完整的标签