Hi Keith,

I read your post and decided to write a function for you. Basically what 
it does is does, is take in a string, and optional newline and value 
separators, and outputs an associative array.

It actually works, and has a few comments... so yeah, enjoy.. and thanks 
for the challenge : )

Adam


You can use it like so...

PHP Script (of course, you must either have the function on the same 
page or include() it like so):

<?php include('extractPairs.php');

header("Content-Type: text/plain");

$strText = "Name1=Value1 \n Name2 = Value 2\nName 3 =Value 3";

$arrPairs = extractPairs($strText);

print_r($arrPairs);

?>

Output:


Array
(
     [Name1] => Value1
     [Name2] => Value 2
     [Name 3] => Value 3
)

//=====> extractPairs.php
<?php

function 
extractPairs($strText,$strValueSeparator="=",$strNewlineSeparator="\n"){
        
        // kill the bad stuff... prevent a meaningless array
        if (!is_string($strText)){ return array(""=>""); }
        
        // gets rid of newlines/spaces etc so we don't get a crap array
        $strText = trim($strText);

        // separate each line in an element in an array
        $arrLines = explode($strNewlineSeparator,$strText);

        // loop through $arrLines, separating the names/values and placing 
them in $arrOutput
        for ($i=0;$i<count($arrLines);$i++){
                $pos = strpos($arrLines[$i],$strValueSeparator);

                // get the text before and after the found position
                // and get rid of the whitespace b4 and after
                $arrOutput[trim(substr($arrLines[$i],0,$pos))] = 
trim(substr($arrLines[$i],$pos+1));
        }
        
        return $arrOutput;
}

?>

> Ok, lets say I have some code here:
>
> $result = array();
>
> $ch = curl_init ("https://www.myverificationplace.com/verify.asp";);
>
> curl_setopt ($ch, CURLOPT_POST, 1);
> curl_setopt ($ch, CURLOPT_POSTFIELDS, $args);
> curl_setopt ($ch, CURLOPT_TIMEOUT, 120); // Set the timeout, in seconds.
> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
> $result = curl_exec ($ch);
>
> curl_close ($ch);
>
> Now that we see that, how can I make it so that the output from curl, 
> which
> is in the variable $result, will be an array, for each line of output?
>
> Some of the line of output looks like this:
>
> ssl_result_message=APPROVED
> ssl_txn_id=00000000-0000-0000-0000-000000000000
> ssl_approval_code=000000
>
> I need each of the lines to be turned into a variable. Any ideas as to 
> how I
> might go about this?
>
> Thanks,
>
> Keith Posehn

Reply via email to