get value - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of get value.php

  1. <?php
  2.  
  3. $html = '
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8. </body>
  9. <script>
  10. var var1 = [{"key1":"value1","key2":"value2"}]
  11. ...
  12. </script>
  13. </html>
  14. ';
  15.  
  16. $target_var = 'var1';
  17.  
  18. preg_match('/var\s*'.$target_var.'\s*=\s*([^\]]+\])/i', $html, $matches);
  19.  
  20. print_r($matches); //test match
  21.  
  22. if(!empty($matches[1]))
  23. {
  24.         $val = json_decode($matches[1]);
  25.  
  26.         print_r($val); //the value
  27. }
File Description
  • get value
  • PHP Code
  • 12 May-2022
  • 383 Bytes
You can Share it: