0

Basically I want to read and write to a file then save all the compiled content another file.

Steps:

  1. Say I have a file mycode.js with this code:

      var url = '';     //this will be field throug form in php
    
  2. Now in php, I will input a value of that url e.g www.google.com

  3. How will I be able to append the url I just entered to my js file?
    Expected output:

    var url = 'www.google.com';
    
  4. Also follow question, upon successful append of the value to my JS file I will now create a new file (newlyappend.js), like a save as thing. Where the content of the file is the newly append code:

    var url = 'www.google.com';
    

Would like to achieve this using php.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Wondering Coder
  • 1,652
  • 9
  • 31
  • 51
  • for what I got from your question, you are basically trying to modify mycode.js and then save the changed file as newlyappend.js? – davidaam Jun 07 '12 at 08:19
  • 1
    I'm not quite getting what you're trying to do here. If you are simply trying to use a PHP variable in JS, you can do it like this: var someVar = '=$my_php_var?>'; – Index Jun 07 '12 at 08:21
  • possible duplicate of [Pass a PHP string to a Javascript variable (including escaping newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines) – lanzz Jun 07 '12 at 08:24
  • If you don't want to put PHP code in your Javascript source and `include` it (that would be the easiest way) but rather want to make a statis file on the server with this value then you could `file_put_contents('newlyappend.js', preg_replace('/^\s*var\s+url\s*=\s*[\'"]{2}\s*;\s*$/m', "var url = '$urlFromPHP';", file_get_contents('mycode.js'));` – DaveRandom Jun 07 '12 at 08:26

3 Answers3

2

Rename your js file to mycode.php and write php code inside the file like this, so php engine will execute the file

var url = "<?php echo($YOUR-URL); ?>";

Now in your html file include this file just like you include js file in the script tag. Give src="mycode.php". Browser will treat it as a js file and you can access the varible url in your javascript.

kiranvj
  • 32,342
  • 7
  • 71
  • 76
2

Get the .js file content as a string:

$js = file_get_contents( '/path/to/file/mycode.js' );

$js will now hold the content of the file.

Replace the empty variable line with a value assignment:

$url = 'www.google.com';
$js = str_replace( "var url = '';", "var url = '$url';", $js );

Write the new content back to a new js file:

file_put_contents( '/path/to/file/newlyappend.js', $js );

Make sure you have proper permissions to read & write files from the specified paths.

Yaniro
  • 1,595
  • 9
  • 14
1

The simplest solution is to just include PHP code inside the <script></script> section of your HTML templates / web pages because chances are, the file extension is .php:

<script>
    var jsVar = "<?php echo $phpVar ?>";
</script>

A solution for this is to make your server parse all files ending in .js. Just create a .htaccess if it doesn’t exist in the directory in which you wish to include and run PHP code inside all .js files. Add these lines at the end of .htaccess:

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js

Also you may use php5-script instead of x-httpd-php5 in some cases. And you may also try this with regular expressions to better match specific criteria:

<FilesMatch "\.(js|php)$">
    SetHandler application/x-httpd-php
</FilesMatch>

Now you can add all the PHP code you can inside any JavaScript files with a .js extension, the server will automatically parse and run them when the client side requests the file. Hope this helps! :)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252