Extracy form values in string

Discussion in 'PHP' started by cloudnthunder, Mar 14, 2010.

  1. #1
    Hi I have an API which return me form values in a string:

    <input name="field1" value="xx"/>
    <input name="field2" value="yy"/>

    The form is stored in a string. Is there a way to extract the value(xx & yy) by the input name?
     
    cloudnthunder, Mar 14, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    $string = <<<eof
    <input name="field1" value="xx"/>
    <input name="field2" value="yy"/>
    eof;
    
    preg_match_all('~<input name="field([1-2]*)" value="(.+)"/>~', $string, $values);
    
    $xx = $values[2][0];
    $yy = $values[2][1];
    
    ?>
    PHP:
     
    danx10, Mar 14, 2010 IP
  3. cloudnthunder

    cloudnthunder Peon

    Messages:
    163
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi danx10 i tried ur code can't seem to work. Basically i need something like this:

    <?php

    $string = '
    <input name="field1" value="xx"/>
    <input name="field2" value="yy"/>
    ';

    echo $extractinput->extract($string, "field1");

    ?>

    how do i make the extract function based on field name and html string?
     
    Last edited: Mar 14, 2010
    cloudnthunder, Mar 14, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    My code works fine:

    <?php
    
    $string = <<<eof
    <input name="field1" value="xx"/>
    <input name="field2" value="yy"/>
    eof;
    
    preg_match_all('~<input name="field([1-2]*)" value="(.+)"/>~Us', $string, $values);
    
    $xx = $values[2][0];
    $yy = $values[2][1];
    
    echo $xx;
    ?>
    PHP:
    $values contains an array, theirfore no matter how many input tags you have you'll always have the value of the input within the array, so use your php knowledge to extract specific values from the array. (just like I've done with $xx and $yy), you can read about arrays on the php website here

    However if you need further assistance, just reply :)
     
    danx10, Mar 14, 2010 IP