1

I have a sting, In that string, my array values are stored. How can I get array value from the string? Is there any predefined function in PHP or will achieve using REGEX?

$rule ="array(
array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
)";

if I do print_r($rule); it's giving string value only same as above. But I want output like

Array ( [0] => Array ( [parameter] => signature name [values] => RNSOC023 [condition] => EQUALS [rule type] => AND ) [1] => Array ( [parameter] => DestinationZone [values] => l3-internet [condition] => EQUALS [rule type] => AND ) [2] => Array ( [parameter] => Application [values] => ftp,sftp [condition] => EQUALS [rule type] => AND ) )
5
  • you theoretical could use the evil eval. But before going that direction: Where is the string coming from? Commented Mar 29, 2018 at 11:16
  • That are rules defined by machine and stored as a string in db Commented Mar 29, 2018 at 11:18
  • 1
    Then you would have to eval it. But I owuld suggest you definitely change the way it is stored. Store it as JSON or serialized, because that can be easily hydrated to the original type (so an array). Commented Mar 29, 2018 at 11:19
  • Just remove the string delimeter " from $rule value. Commented Mar 29, 2018 at 11:26
  • If the values stored in db won't change anymore I suggest to run a 'converting' script once to get usable values into your db. If that 'machine' keeps updating/adding to db and you cannot change that - eval is the left over choice. Maybe think about suing the 'developer' who made the machine do that... Commented Mar 29, 2018 at 11:27

5 Answers 5

3

Finally got the solution, Try this:

$rule ="array(
    array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
    array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
    array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
    )";
    $returnValue = '';
    eval("\$returnValue = $rule;");
    print_r($returnValue);
Sign up to request clarification or add additional context in comments.

2 Comments

have you tried it?? Results in array(1) { [0]=> string(324) "array( array('parameter'=>...
Again remain content getting string only
3

eval() is EVIL! It renders your code open to all sorts of security issues and should be avoided at all costs. Try this instead:

$rule ="array(
array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
)";
preg_match_all("/(?:array\()('.*?'\s*=>\s*'.*?'\s*)+(?:\))/", $rule, $matches);
$rulearray = array();
$i = 0;
foreach ($matches[1] as $value) {
    preg_match_all("/('.*?'\s*=>\s*'.*?')/", $value, $m);
    foreach ($m[1] as $e) {
        list ($k, $v) = preg_split('/\s*=>\s*/', $e);
        $rulearray[$i][$k] = $v;
    }
    $i++;
}
print_r($rulearray);

Output:

Array ( [0] => Array ( ['parameter'] => 'signature name'
                       ['values'] => 'RNSOC023'
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' ) 
        [1] => Array ( ['parameter'] => 'DestinationZone' 
                       ['values'] => 'l3-internet' 
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' ) 
        [2] => Array ( ['parameter'] => 'Application'
                       ['values'] => 'ftp,sftp'
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' )
)

1 Comment

thanks dude, just learnt a new way to keep an array in string format <3
0

The approach you are using to keep array of data in a database is wrong, You should use json string format in the database and when getting data, you can json_decode function in php to convert into array. Try to convert that array into json string and store in the db. It's the best practise. That's why you are getting these types of issues.

2 Comments

Agree, But in my scenario, I have to do like this only
why? where is the place that you are going to save array ? Or is it existing from beginning ?
0

So basically you would want json_encode() to make the array. What I tested now was the following:

$rule = array(array('parameter1'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'), 1 => array('parameter2'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'), 2 => array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND'));
$jr = json_encode($rule);
$jd = json_decode($jr, true);
print $jd[0]['values'];

The above worked, and tested on my environment.

For more information on json_encode(); and json_decode();

If you run print_r($jd); you will get an output of:

Array ( [0] => Array ( [parameter1] => signature name [values] => RNSOC023 [condition] => EQUALS [rule type] => AND ) [1] => Array ( [parameter2] => DestinationZone [values] => l3-internet [condition] => EQUALS [rule type] => AND ) [2] => Array ( [parameter] => Application [values] => ftp,sftp [condition] => EQUALS [rule type] => AND ) ) 

1 Comment

You have to take in the string. you removed the quotations
0

The code will be executed in the scope of the code calling eval(). Thus any variables defined or changed in the eval() call will remain visible after it terminates. For More Info HERE

eval("\$rule = $rule;");
print_r($rule);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.