Parse a JavaScript file through PHP
<script src="/path/to/my/file.php"></script>
In file.php
you'll also want to output the correct header, before outputting anything you should have the following:
header("Content-Type: application/javascript");
EDIT: As @Tony_A pointed out, it should be application/javascript
. I don't think it mattered as much when I wrote this post in 2010 :)
Best way to parse a Javascript file in PHP to extract the array defined inside it
You are looking for something like this. Note I had the .js file as local so I used file()
to load it into array. For your actual script you'll probably need file_get_contents()
if your php can't access locally the .js file.
<?php
$lines = file('test.js');
$pages = array();
foreach($lines as $line) {
if(strpos($line, 'new Array') != false) {
preg_match('/Page\[\d\]\s?\=\s?new Array\((\"(.*)",?\s?\n?)+\);/', $line, $matches);
$values = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $matches[1]);
$currNo = count($pages);
$pages[$currNo] = array();
for($i = 0; $i < count($values); $i++) {
array_push($pages[$currNo], trim($values[$i], '"'));
}
}
}
var_dump($pages);
For your example the result will be the following:
array(2) {
[0]=>
array(4) {
[0]=>
string(10) "Some text1"
[1]=>
string(10) "More text1"
[2]=>
string(11) "Final Text1"
[3]=>
string(8) "abc.html"
}
[1]=>
array(3) {
[0]=>
string(10) "Some text2"
[1]=>
string(10) "More text2"
[2]=>
string(8) "xyz.html"
}
}
Enjoy!
Parse a JS script using PHP and REGEX to get a JS variable value
In this case, the end can be found by matching a semicolon at the end of a line:
if (preg_match('/^var pveapi = (.*?);$/ms', $js, $matches)) {
$data = json_decode($matches[1]);
print_r($data);
}
Parse js/css as a PHP file using htaccess
Try:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
using php in js function (external .js file)
It's possible to have PHP parse/interpret your .js
files just like it does with .php
files, but it's generally a bad idea. (And certainly no PHP installation would do it by default.)
Instead, put the value you want in your .php
file before your external JavaScript executes, and just reference it in your external JavaScript. For example:
<script type="text/javascript">
var test1 = <?php echo x; ?>;
</script>
<script type="text/javascript" src="js/jakosc-powietrza.js"></script>
As the complexity grows there are various ways to manage scope, treating the dynamic value(s) as a dependency to be injected into an otherwise self-contained (unit testable) JavaScript module. But the premise remains the same.
Related Topics
Mcrypt_Encrypt to Openssl_Encrypt, and Openssl_Zero_Padding Problems
Parse a JavaScript File Through PHP
Laravel 5.2 $Errors Not Appearing in Blade
Smtp Configuration for PHP Mail
Recursively Remove Empty Elements and Subarrays from a Multi-Dimensional Array
How to Check If String Is a Valid Xml Element Name
Why Does the Laravel API Return a 419 Status Code on Post and Put Methods
Populate a Drop Down Box from a MySQL Table in PHP
PHP - Split String in Key/Value Pairs
PHP Default Function Parameter Values, How to 'Pass Default Value' for 'Not Last' Parameters
Comma Separated Values in MySQL "In" Clause
Advantages of MySQLi Over MySQL
Python Equivalent of PHP's Compact() and Extract()
PHP MySQL Search Multiple Tables Using a Keyword