【问题标题】:How to escape quote PHP strings generated by Delphi?如何转义 Delphi 生成的引用 PHP 字符串?
【发布时间】:2010-07-15 05:54:24
【问题描述】:

我的代码里到处都是这样的东西

    Write  (thePhpFile, '    echo "<option value=\"' +
                                    theControl.Name +
                                    '_selection\">" . $' +
                                     theControl.Name +
                                     '_values[$_POST["' +
                                      theControl.Name +
                                      '_selection"]];');

生成以下 PHP 代码

 echo "<option value=\"ComboBox1_selection\">" . 
                      $ComboBox1_values[$_POST["ComboBox1_selection"]];?>

肯定有更好的方法来处理这个问题吗?也许是一个 Delphi 函数,我可以传递所需的 PHP 字符串并在必要时将其加倍引号?还是我自欺欺人?

基本算法似乎是

// assume all PHP strings are double quoted, even when it's inefficient
find the first double quote, if any
find the last double quote, if any  
for every double quote in between
   change it to \""

听起来对吗?也许我应该自己编码?

嗯,没那么容易......在上面的 sn-p 中,我们不想逃避 $_POST["ComboBox1_selection"] - 最好只是手动编码顶部显示的混乱?

有什么想法吗?我现在要去谷歌“从delphi生成php”——这可能是我几周前应该做的:-/

【问题讨论】:

    标签: delphi escaping


    【解决方案1】:

    您不只是在生成 PHP。您正在生成 PHP,而 PHP 又会生成 HTML。您的程序需要了解两种语言的字符串编码规则。为此编写两个单独的函数可能会更容易:

    function QuoteStringForPHP(s: string): string;
    function QuoteHTMLAttributeValue(s: string): string;
    

    根据您的示例,您可以像这样使用它们:

    ControlName := theControl.Name;
    ValueName := ControlName + '_values';
    SelectionName := ControlName + '_selection';
    Write(thePhpFile, '    echo ' +
      QuoteStringForPHP(
        '<option value=' +
          QuoteHTMLAttributeValue(SelectionName) +
        '>'
      ) +
      '.' +
      '$' + ValueName + '[$_POST[' +
        QuoteStringForPHP(SelectionName) +
      ']];'
    );
    

    编写它们很简单:

    // Input: Any sequence of characters
    // Output: A single-quoted PHP string literal
    function QuoteStringForPHP(s: string): string;
    begin
      // Commented apostrophes are only to fix Stack Overflow's code highlighting
      s := StringReplace(s, '\' {'}, '\\', [rfReplaceAll]);
      s := StringReplace(s, '''', '\''' {'}, [rfReplaceAll]);
      Result := '''' + s + '''';
    end;
    
    // Input: Any sequence of valid HTML text characters
    // Precondition: s has not already had any HTML encoding applied to
    //               it; i.e., no character references
    // Output: A single-quoted HTML attribute value
    function QuoteHTMLAttributeValue(s: string): string;
    begin
      s := StringReplace(s, '&', '&amp;', [rfReplaceAll]);
      s := StringReplace(s, '''', '&apos;', [rfReplaceAll]);
      Result := '''' + s + '''';
    end;
    

    我选择使用撇号而不是引号,因为这使 PHP 转义更容易。如果您对 PHP 字符串使用引号,那么您需要担心变量插值。有了撇号,这个问题就迎刃而解了。

    通过让QuoteStringForPHP 负责引用转义,您可以轻松转义。无需再猜测哪些角色需要转义,因为它们所有都需要转义。不需要转义的只在转义工作完成后添加。

    【讨论】:

    • +1 哇,Rob,这看起来很棒!我会尽快给它一个测试旋转。 PHP 中的单引号没有问题,我更喜欢它们(原因很明显)。
    【解决方案2】:

    我已多次阅读您的问题,但我无法理解您的问题是什么。我会保留代码原样。

    但是您似乎不想在您的代码中使用这个小而烦人的转义字符?正如您所描述的,转义算法非常容易出错。它试图解决没有语义信息的句法问题。但是你如何为解析算法提供这些信息呢?对,使用转义字符。 但是您似乎不想在代码中使用这个微小但令人讨厌的转义字符?正如您所描述的,转义算法非常容易出错。它试图解决没有语义信息的句法问题。但是你如何为解析算法提供这些信息呢?对,使用转义字符。 但是您似乎不想在代码中使用这个微小但令人讨厌的转义字符?正如您所描述的,转义算法非常容易出错。它试图解决没有语义信息的句法问题。但是你如何为解析算法提供这些信息呢?对,使用转义字符。 ...

    ;-)

    【讨论】:

    • +1 叹息,我想我不得不同意你的观点,但是有数百个,可能达到四位数,其中每个我都要问自己,我是男人吗”或\"或\""
    • 我希望我能给机智+2 :-)
    【解决方案3】:

    好吧,我不知道你的能力,所以我不知道你是否可以自己做。这并不完全是微不足道的(因为字符位置由于插入而改变),但我会说这是第一个编程类的问题。 (注意那些不变量!)

    function escapephp(s: string):string;
    var first,n,cur,lastcur : integer;
    begin
      result:=s;
      first:=pos('"',result);
      if first>0 then   // first found? 
        begin
          n:=0;     
          cur:=first;
          while cur<>0 do
            begin
              lastcur:=cur;   // keep last found
              cur:=posex('"',result,lastcur+1);  // search next
              if  (lastcur<>first) and (cur<>0) then // next is not last and not first?
                begin
                  insert('\"',result,lastcur);  // then insert
                  inc(cur,2);                   // we inserted 2 chars so need to correct with 2
                end;
           end;
       end;
    end;
    

    该示例可以进行更彻底的测试,我只是在您的字符串上快速测试了它

    【讨论】:

    • +1 谢谢!是的,当然,我可以做到 - if 我可以清楚地定义需求,但这可能意味着定义 PHP 语法。我会在几个例子上测试你的代码,如果它有效,我会奖励答案,但我担心我可能不得不把所有东西都交给 cdoe。
    • 据我所知,他想将这样的'echo "&lt;option value="ComboBox1_selection"&gt;" . $ComboBox1_values[$_POST["ComboBox1_selection"]];' 转换为'echo "&lt;option value=\"ComboBox1_selection\"&gt;" . $ComboBox1_values[$_POST["ComboBox1_selection"]];' 的问题
    猜你喜欢
    • 2010-09-23
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2012-11-10
    • 2012-03-12
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多