Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 925769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:34:18+00:00 2026-05-15T19:34:18+00:00

My code is littered with things like this Write (thePhpFile, ‘ echo <option value=\’

  • 0

My code is littered with things like this

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

which genrates the following PHP code

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

Surely there’s a better way to handle this? Maybe a Delphi function which I can pass the desired PHP string and have it double up quotes where necessary? Or am I deluding myself?

The basic algorithm would seem to be

// 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 \""

Does that sound right? Maybe I should code it myself?

Hmmm, not so easy … in the snippet above, we wouldn’t want to escape $_POST["ComboBox1_selection"] – better just to hand code the mess shown at the top?

Any ideas? I’m off now to google “generating php from delphi” – which is probably what I should have done weeks ago :-/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:34:19+00:00Added an answer on May 15, 2026 at 7:34 pm

    You’re not just generating PHP. You’re generating PHP that in turn generates HTML. Your program will need to be aware of both languages’ string-encoding rules. It will probably be easier to write two separate functions for that:

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

    Given your example, you’d use them like this:

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

    Writing them is straightforward:

    // 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;
    

    I chose to use apostrophes rather than quotation marks since that makes PHP escaping easier. If you use quotation marks for your PHP strings, then you need to worry about variable interpolation. With apostrophes, that problem goes away.

    By making QuoteStringForPHP responsible for quoting and escaping, you make escaping easy. There’s no more guesswork about which characters need escaping because they all do. The ones that don’t need escaping are only added after the escaping work is complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have functions that look like this that is littered through out the code
Working on existing code, I recently found smt like this: if(Config.ExceptionBehavior.DEBUG_EXCEPTIONS) { foo.Foo(); }
Code to create new form instance of a closed form using form name I
Code sample for my question: IList<TestDataAnimal> testDataFromDb = this.db.TestDataAnimals.Include(t => t.TestType).Include(t => t.Visit).Include(t =>
Code: $exchangesnapin = Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010; $output = shell_exec('powershell '.$exchangesnapin.';get-mailboxdatabase 2>&1'); echo( '<pre>' ); echo(
The code base I'm currently working on is littered with hard-coded values. I view
I mean everywhere in code example on internet it's littered with tid, vid etc.
Code <c:if test=#{attr1 && !attr2 && (empty attr3)}> Error returned The entity name must
After maintaining lots of code littered with #region (in both C# and VB.NET), it
Our code uses a lot of system properties eg, 'java.io.tmpdir', 'user.home', 'user.name' etc. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.