Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

echo variables to screen

want to store values in variables and echo to screen
so I can save these variables
see related question

want
echo $timestamp $costvariable $cost $productid
<?php
$string="05/26/2011 03:26:58PM | dbquery | | SQL: UPDATE products SET cost2 = '372' WHERE productid = '151951' | Error: test\n05/26/2011 05:44:48PM | dbquery | | SQL: UPDATE products SET cost3 = '445.00' WHERE productid = '151529' | Error: test";

/**
* @param String $string is the source string
* @param Array  $fieldNames is the basename for all
*               fields that needs to be matched. When 
*               provided with array('cost', 'productid') the
*               function will match 'cost', 'cost1', 'cost2',
*               etc, 'productid', 'productid1', etc.
*
*/
function findFields($string, $fieldNames) {
    
    // Local variable to capture return value
    $result = array();
    
    // Part of the regular expression that will match field names.
    $q = implode('|', array_map('preg_quote', $fieldNames));
    
    // Regular expression that will find the variables and values
    $regexp = '/(('.$q.')\d*)\s*=\s*([\'"])([^\\3]*?)\\3\s*/';
    
    // Run through all lines
    foreach (explode("\n", $string) as $lineNumber => $line) {
        
        // Make the line human readable. Escape this line
        // for easier processing in later stages
        $lineNumber = 'Line#'. ($lineNumber + 1);
        
        // Explode into cells to get the timestamp / sql columns
        $array = explode('|', $line);
        
        // Store timestamp
        $result[$lineNumber] = array('timestamp' => $array[0]);
        
        // Process all variable findings
        if (preg_match_all($regexp, $array[3], $m)) {
            foreach($m[1] as $index => $field) {
                $value = $m[4][$index];
                $result[$lineNumber][$field] = $value;
            }
        }
    }
    
    return $result;
}

$results = findFields($string, array('cost','productid'));

echo '<pre>';
print_r($results);
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

your echo statement knows to use $cost2

it should only know to use cost1, cost2, cost3 by the contents of $string
so I think there should be a variable of the number after the word cost
This is EXACTLY the problem I wrote about in the other question/response, where I wrote something with meaning like "creating more variable names creates more problems."  Please consider hiring a professional to help you with this.  You're at the threshold of a terribly bad experience.  Please step back now before it is too late.

A design pattern that creates more variable names instead of array elements is technically incompetent.  Please consider using an array instead of individual variables.

We have corresponded on a lot of questions here at EE.  Good luck, ~Ray
Avatar of rgb192

ASKER

i have a table with productid and cost1,2,3
so your code works when I put in if statements to tell whether cost1,2,3 is used.
Maybe I can fix table so there is no cost1,2,3 but at least there is no productid1,2,3.



Thank you for helping me.