Perhaps someone can shed some light on which approach is best between parse_str, explode or preg_split for the following.

From a 'static' HTML page, I will have a lengthy String with 12 'fields' contained one 'hidden' field "value" ... which must get parsed into $variables in a PHP script for some data manipulation.

HTML example:

<input type="hidden" name="test" value="aa^bb^cc^dd^ee^ff^gg^hh^ii^jj^kk^ll">

In the PHP script, I need to:

  1. Parse out data contained between the ^ delimiters (also at front & end) into...

  2. Separate $variables like... (aa) as $aa, (bb) as $bb, etc.

  3. If there is nothing between any of the delimiters, then like $ff = " " (etc.)

  4. In one of the 'fields' in the string, there may be one or more sub-string values to parse out, like: ... op1op2op3 (as many as three HTML "Option" results from drop-down lists), into $op1, $op2 & $op3 ... which may later get combined with one of the other $variables. I guess this is like a 'sub-parsing' within the overall parsing?

  5. Some 'fields' will be text, some numbers.

I'm sure this is possible, but stuck on which php function to use.

Thanks for any guidance.

    hi,
    first of all:

    5. Some 'fields' will be text, some numbers.

    Will not be a problem at all.
    to get the value attribute of the input tag named test, use this - of course, u gotta replace the path to the html file in the file command:

    // read the static page into $html
    $html = implode("", file("mystaticpage.htm"));
    
    $matches = array();
    
    if(preg_match("/\\<input type\=\"test\" value=\"(.+?)\"\\>/i", $html, $matches))
    {
    $inputValue = $matches[1];
    }
    else
    {
    echo "Pattern did not match";
    }
    

    Then, to split the value, use:

    $variables = explode("^^", $inputValue);
    

    However, note that i wrote this from the top of my head, so it might work or might not, feel free to ask if something runs wrng, especially with the regexp pattern matching..l.
    So far, u have the values of the input tag split in the array variables... the rest seems rather difficult or useless to me:

    2. Separate $variables like... (aa) as $aa, (bb) as $bb, etc.

    How do u want to access the variables if u give em their values as names... i would not know why to do that....

    4. In one of the 'fields' in the string, there may be one or more sub-string values to parse out, like: ... ^op1op2op3^ (as many as three HTML "Option" results from drop-down lists), into $op1, $op2 & $op3 ... which may later get combined with one of the other $variables. I guess this is like a 'sub-parsing' within the overall parsing? 
    

    what is the criteria for the "sub-parsing"?? the numerical index? or 3 chars?? u gotta define something that allows php to see where to cut the string....
    hth

      Hi Natty:

      Thank you for your kind reply.

      I did some additional thinking, and decided to increase the number of fields to eliminate any 'sub-parsing' (within a particular field).

      OK...here is a sample of the HTML page wherein (for the Example) several options will be selected: Size=XL, Color=White, Inscription="Good Luck!".

      <html>
      <head>
      <title>Test</title>
      </head>
      
      <FORM action="process.php" method=POST>
      
      <INPUT TYPE="hidden" name="item" VALUE="TS^SFP-1^Stanley Shirt^12.950^qty^Super
      ^prodsize^prodcolor^inscribe^.5^^^NC^^^^yes^^^[url]http://www.somedomain.com/images/prodpix1.gif[/url]">
      
      Quantity:<INPUT NAME=qty size=2 value="1">
      
      Size:
      <SELECT NAME=prodsize>
      <OPTION 1>XL
      <OPTION 2>Large
      <OPTION 3>Medium
      <OPTION 4>Small
      </SELECT>
      
      Color:
      <SELECT NAME=prodcolor>
      <OPTION 1>Red
      <OPTION 2>White
      <OPTION 3>Blue
      </SELECT>
      
      Inscription: <INPUT NAME=inscribe type="text" size=10 maxlength="10">
      
      <br><br>
      <INPUT NAME="submit" type="submit" name="Process">
      </FORM>
      
      <body>
      </body>
      </html>
      

      For some reason, the 'Submit' button does not read "Process" but rather 'Submit Query' which I find quite odd...never had that happen before.

      The 'hidden' field info is to be parsed in 'process.php' which my skeleton of is:

      <?php
      
      // <INPUT TYPE="hidden" name="item" VALUE="TS^SFP-1^Stanley Shirt^12.950^qty^Super
      // ^prodsize^prodcolor^inscribe^.5^^^NC^^^^yes^^^[url]http://www.somedomain.com/images/prodpix1.gif[/url]">
      
      // CODE HERE to parse the above (EXAMPLE) string into $variables below.  
      // No entry between ^^ delimiters will = " "; $prodtype = "TS"; $prodnum = "SFP-1"; $prodname = "Stanley Shirt"; $unitprice = $12.950"; $quantity = "1"; $model = "Super"; $prodsize = "XL"; $prodcolor = "White"; $inscription = "Good Luck!"; $shipweightunit ".5"; $shipcosteach = " "; $insurefee = " "; $qtychange = "NC"; $noshipcost = " "; $notaxcost = " "; $digital = " "; $invupd = "yes"; $future1 = " "; $future2 = " "; $prodpix = "http://www.somedomain.com/images/prodpix1.gif"; // DATA MANIPULATION will take place here with further processing // on subsequent php pages. This may require "Sessions"? FORM ??? ?>

      So, if a delimited field contains nothing, the $variable must have a null value (" ").

      Later in the processing, I need to append $model, $prodsize, $prodcolor & $inscribe TO $prodname with " - " between.
      Or, create another $variable like:

      $prodfull = "Stanley Shirt - Super - XL - White - Good Luck!"

      Or, maybe: $prodfull = "Stanley Shirt:Super:XL:White:Good Luck!

      Does this make sense?

      Umh, in the post 'hidden' fields for the URL looks messed up I think...it is supposed to have an h t t p : / / just the link value as showing in the $prodpix = ($variable example).

      Thanks much.

        Natty:

        I tried the code you posted in a 'process.php' file (without my example $variable assignments):

        <?php
        
        // read the static page into $html 
        $html = implode("", file("select.html")); 
        //NOTE: file is in the same directory
        
        $matches = array(); 
        
        if(preg_match("/\<input type=\"test\" value=\"(.+?)\"\>/i", $html, $matches)) 
        { 
        $inputValue = $matches[1]; 
        } 
        else 
        { 
        echo "Pattern did not match"; 
        }
        
        $variables = explode("^^", $inputValue);
        
        // DATA MANIPULATION will take place here with further processing
        // on subsequent php pages.  This may require "Sessions"? FORM ???
        
        ?>
        

        Here's what I got:

        Pattern did not match Notice: Undefined variable: inputValue in process.php on line 17
        

        Line 17 is:

        $variables = explode("^^", $inputValue);
        

        Do I need to use some kind of 'loop' here? I'm not sure how to assign the values to the specific $variable names (see a previous post).

        I also tried putting the "" at the beginning and the end of the 'hidden' string but still got the same error.

        Thanks.

          FYI, I just ran across this on php.net and wonder if it might be relevant? If so, I just dunno how to fit it in 🙁

          Here's a function I find useful from time to time.... When you're assigning the results of an explode() to a list(x,y,z) of values, you'll get an error/warning in PHP if your explode() didn't return the same number of arguments as your list... So you can use the following simple function to always return the correct number of elements in your array:
          
          // explodeForce - like explode, but rather than just 'limit' the explosion to
          // the number of values, it also ensures that this number of elements are
          // present in the output array - v.useful for when assigning an explode
          // to a list($a,$b,$c)
          
          function explodeForce($sep, $array, $number_values, $pad = '')
          {
              return array_pad(explode($sep, $array, $number_values), $number_values, $pad);
          

            no, i don't think that this function will help u... the pattern does not match since u changed the name of the hidden field from test to item - u gotta change the pattern appropriately:

            if(preg_match("/<input type=\"hidden\" name=\"item\" value=\"(.+?)\">/i", $html, $matches))  

            Moreover, i think your approach using these as delimiters is rather complicated since u gotta store structured data so parsing becomes complex and thus errors are hard to avoid... I thought about this solution:
            U know how to use hashtables in php?? They r arrays with a string instead of a numberical index, like this:

            $test = array("size" => "XL",
                                  "type" => "SFP-1");
            // to read the size, use:
            $size = $test["size"];
            

            i think if u store your data in some hashtable, it's easy to access 'em and also quite straightforward to store 'em / read 'em from the hidden tag:
            To store (a string representation of) the array in some var, use $data = serialize($test);, to restore the array, u gotta write $test = unserialize($data);
            However, i've just noticed that u e.g. store the price in the page the user submits - that's very insecure unless u check the price again after the submission of the data. Since the html page with the price and other information is on the client side, the client has full control: he may (with little knowlegde) manipulate any information you store in the page....
            So maybe it's best to learn usin php sessions (www.php.net/session)...
            they allow u to store information on your server while the client is only recognized via the session id - so he cannot change values without your permission.
            have a look at it
            hth

              Hi Natty:

              Well, I tried the 1st code and how have:

              <?php
              
              session_start();
              
              // read the static page into $html 
              $html = implode("", file("select.html")); 
              
              $matches = array(); 
              
              if(preg_match("/<input type=\"hidden\" name=\"item\" value=\"(.+?)\">/i", $html, $matches)) 
              { 
              $inputValue = $matches[1]; 
              } 
              else 
              { 
              echo "Pattern did not match"; 
              }
              
              $variables = explode("^^", $inputValue);
              
              // DATA MANIPULATION will take place here with further processing
              // on subsequent php pages.  This may require "Sessions"? FORM ???
              
              ?>
              

              But still get:

              Pattern did not match.
              

              I also tried changing the explode from "" to "" again but no luck.

              I have planned to use: session start(); in the process.php page for subsequent pages, but not the initial HTML (as may provide for an opportunity to change price).

              Regarding Hash arrays...I have tried to understand these but not yet able after reading various examples.

                Natty:

                I tested this:

                // TEST
                $test = array("size" => "XL", 
                                      "type" => "SFP-1"); 
                // to read the size, use: 
                $size = $test["size"]; 
                $type = $test["type"];
                
                echo "$size".":"."$type";
                

                And got: XL:SFP-1 which will work.

                However, I simply do not understand HOW to get data from the 'hidden' field string INTO an "array".

                For other purposes, I must use the "" delimiter in the string.

                What to do now?

                Thanks.

                  Yippeee...

                  FYI, got a one-line-of-code solution from a chap on another forum:

                  list($prodtype,$prodnum, etc., etc. ) = explode("^", $_POST['item']); 
                  

                  This works like a charm, except 4 fields from the HTML form do not get values passed...only the 'name' of the text input & select options.

                  Any thoughs on this?

                  Thanks for your assistance.

                    Write a Reply...