-1

I've got an external php file containing the following data:

<?php

/*|[{"username":"user","password":"password","project":"Template"}]|*/

?>

How do I pass the password element into the variable $password? Using my own php script?

Please note that the content of this PHP file has surrounded itself with a comment and pipe symbol. How do I go about removing these so that loading the file adds the json rather than ignoring it.

Daniel Foreman
  • 105
  • 2
  • 6

1 Answers1

1

Here is an example of how to use the json_decode() function:

//$str = file_get_contents('path/to/phpFile.php');

$str = '<?php

/*|[{"username":"user","password":"password","project":"Template"}]|*/

?>';

$str = preg_replace('/<\?php\s{0,}\/\*\||\|\*\/\s{0,}\?>/', '', $str); //<--removes unwanted stuff.

$myArray = json_decode($str, true);

$username = $myArray[0]['username'];
$password = $myArray[0]['password'];

echo $username . '<br>';
echo $password . '<br>';

echo '<pre>';
print_r($myArray);
echo '</pre>';
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
  • Thank you this was a great example and works well with the $str version. However the PHP file I'm using appears to have surrounded itself with a comment and | character. I don't know why, this is just how the open source project i'm trying to interface with works. This means that when I load the file, there's basically nothing for the json_decode to decode because all it see's is a comment. – Daniel Foreman Jun 04 '18 at 08:03
  • @Daniel Foreman Ok run the current code, should work now. – Joseph_J Jun 04 '18 at 08:22