I have a string which have grepped out of a php file, which was the arguments passed into a function, a var_dump of the string looks like this
'foo bar', ['name' => 'John, Smith']
and what I would like to do, is pass this to a function as two arguments. If there wasn't going to be a chance of a , in the array then I could explode the string and pass the two arguments to the function. But I can't and need to parse the string properly.
I can get around this by calling eval.
eval("myfunction({$string});");
which would work, but using eval is "risky". Is there a native method to pass a string to which would separate it into an array of arguments which I could then pass to my function?
I can't run this other php file, or change the code of it, I just want to grep out the arguments (which I have done) and then pass these arguments to my own function.
Edit:
So the string which is greped could be changed, my code for it would need to handle most ways of writing php arguments (no spaces after ',', or ' or "). It is always a string and an array, which is extracted from the text.
So it would need to support things like;
"foo",['name' => 'John, Smith']
"foo",[]
"foo, bar", [1,2 ,3]
Sort of like a reverse var_export... var_import :)
So, it seems like the best way might be to use eval... The code is "trusted" but still feels like a dirty way to do it, and thought there would be a method like parse_str or parse_url.
Edit 2:
So basically, twofiles. a.txt which is opened and regex grabs the params in a function. File b.php runs the regex, and then needs to handle the string to pass to a different function.
a.txt can't be changed to a php file. Need to run that code. (eval works as above).
File a.txt
<?php
... something ...
$result = resetResult('foo bar', ['name' => 'John, Smith']);
.. something ...
?>
File b.php
<?php
$fileContents = file_get_contents('a.txt');
preg_match_all('/resultResult\((.*?)\)/', $fileContents, $matches);
foreach($matches[1] as $args) {
// args = 'foo bar', ['name' => 'John, Smith'] but as a string.
// need to split args so 'foo bar' can be passed as first argument, and the array as the second argument..
someFunction($args[0], $args[1]);
}
I did my best, but I'm still at beginning with PHP. There is no official function to convert an "array string" to array, so I tried to do a workaround with json_decode().
Here is the code and a fiddle
<?
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$original = "'foo, bar', ['name' => 'John, Smith' , 'anotherKey' => 'another value']";
$str = get_string_between($original, "'", "'");
$arr = get_string_between($original, "[", "]");
$arr = json_decode(str_replace(' => ',':',str_replace('\'','"','{'.$arr.'}')), true);
var_dump($str);
print_r($arr);
?>
Output
string(8) "foo, bar"
Array
(
[name] => John, Smith
[anotherKey] => another value
)
then call yourFunc($str,$arr).
Of course my code could be improved, it's just a start. There will be a problem if an array value will contains =>, this should be fixed.
P.S.: get_string_between() was taken here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With