【问题标题】:Converting an ASP function to PHP将 ASP 函数转换为 PHP
【发布时间】:2015-11-11 09:00:29
【问题描述】:

我正在将录音室网站从基于 Windows 的服务器迁移到 linux 的服务器。除了使用 ASP 函数更新新版本的一页之外,它都是 html 和 php。 我一直在尝试使用“echo”自己转换它,但我什至无法接近工作代码。 这里是:

<%

Function PrintRecord(strImage, strAutore, strTitolo, strInfo, strCredits)
    dim strRet
    strRet = strRet + " <td valign=""top"">"
    strRet = strRet + " <div style=""margin-left: 20px""> "
    strRet = strRet + "     <img src=""pictures_works/" + strImage + """ height=""80"" width=""80"" border=""1"">"
    strRet = strRet +"  </td>"
    strRet = strRet + " <td width=""170px"" valign=""top"">"
    strRet = strRet + "     <font class=""TestoPiccoloNo"">"
    strRet = strRet + "         <b>" + strAutore + "</b><br>"
    strRet = strRet + "         " + strTitolo + "<br>"
    strRet = strRet + "         " + strInfo + "<br>"
    strRet = strRet + "<i>- " + strCredits + " </i>"
    strRet = strRet + "     </font>"
    strRet = strRet + " </div> "
    strRet = strRet + " </td>"
    PrintRecord = strRet
End Function

%> 

这是我用来更新的代码:

<%=PrintRecord("somepic.jpg","someband","somerecord","somelabel","whodidwhat")%>

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 你让我穿越到了 10 年前。
  • 这正是网站创建的时间!

标签: php asp.net code-conversion


【解决方案1】:
  • 将每个+ 替换为.
  • 为变量添加前缀$
  • 用; 终止行
  • $strRet = $strRet . 可以缩短为 $strRet .=
  • 调整开始和结束标签
  • 用大括号括起来函数
  • 将"" 替换为\"

<?php

function PrintRecord($strImage, $strAutore, $strTitolo, $strInfo, $strCredits) {
    $strRet = '';
    $strRet .= " <td valign=\"top\">";
    $strRet .= " <div style=\"margin-left: 20px\"> ";
    // :
    // ...similar
    // :
    $strRet .= "         <b>" . $strAutore . "</b><br>"; // example for concatenation
    // :
    $strRet .= " </div> ";
    $strRet .= " </td>";
    return $strRet;
}

?>

和

<?php echo PrintRecord("somepic.jpg","someband","somerecord","somelabel","whodidwhat"); ?>

【讨论】:

  • 你忘记了$strAutore的签名
  • 谢谢,已更正。我一直在与格式问题作斗争。
  • 通过对代码的一些语法更改让它工作。 Html标签需要在“'”之间 我要编辑你的答案吗?谢谢!
  • 如果你愿意(如果可能的话),不反对。但我认为更重要的部分是上面的变化列表。
【解决方案2】:

在 PHP 中你可以做多行字符串。

<?php

function PrintRecord($strImage, $strAutore, $strTitolo, $strInfo, $strCredits){
  $strRet = '
    <td valign="top">
      <div style="margin-left: 20px">
      <img src="pictures_works/'.$strImage.'" height="80" width="80" border="1">
      </div>
    </td>
    <td width="170px" valign="top">
     <div>
       <font class="TestoPiccoloNo">
          <b>'.$strAutore.'</b><br>
          '.$strTitolo.'<br>
          '.$strInfo.'<br>
          <i>- '.$strCredits.'</i>
       </font>
    </div>
   </td>';
  return $strRet;
}
?>

或通过 HEREDOC 语法:

<?php
function PrintRecord($strImage, $strAutore, $strTitolo, $strInfo, $strCredits){
    $strRet = << EOT
    <td valign="top">
      <div style="margin-left: 20px">
      <img src="pictures_works/{$strImage}" height="80" width="80" border="1">
      </div>
    </td>
    <td width="170px" valign="top">
     <div>
       <font class="TestoPiccoloNo">
          <b>{$strAutore}</b><br>
          {$strTitolo}<br>
          {$strInfo}<br>
          <i>- {$strCredits}</i>
       </font>
    </div>
   </td>
   EOT;
  return $strRet;
}
?>

并像这样使用它:

<?php echo PrintRecord("somepic.jpg","someband","somerecord","somelabel","whodidwhat");?>

嗯,它看起来怎么样?

【讨论】:

  • 用heredoc syntax会更方便。
  • @syck :是的,可以
  • 您缺少echo 或&lt;?=
  • 完美运行!您只是在第一行末尾错过了{
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2022-01-04
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多