1

Using PHP, I want to be able to open a JavaScript file and take out a part of a particular line. Using the example below, I would want to remove "8:20am|8:20am" from the file, keeping everything else intact.

My JavaScript file looks like this:

var daylist = document.checkout.checkoutDay
var timelist = document.checkout.times
var times = new Array()
times[1]=["8:00am|8:00am", "8:20am|8:20am", "8:40am|8:40am", "9:00am|9:00am"]

There may be multiple lines in the Array and each with different times, so I won't know exactly where to find the string. I was thinking of maybe using str_replace() and fopen() to open the file, but without knowing what the line is going to look like ahead of time, I wouldn't know what string to replace. Any ideas?

4 Answers 4

1

Try this:

<?php
$file = file_get_contents( 'MY_FILE.JS' );

$search = array( '"8:20am|8:20am"' );
$replace = array( '' );

$file = str_ireplace( $search, $replace, $file );
$file = preg_replace( '/(,\s*,)/', ',', $file ); // remove 2 consecutice commas.

file_put_contents( 'MY_FILE.JS', $file );
?>

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This is great, but there may be multiple "8:20|8:20" on different lines. I just want to remove the 8:20|8:20 on time[1] only.
In that case, you have to pass more intelligence to the replace function. Which one to remove, preg_replace will be the preferred one. I must say it will be difficult. Can't you just delete this variable from your database / source from you are populating the js file in the first palce? That would be a lot easier. :)
1

Manipulating the JavaScript file with PHP means that the JavaScript is no longer a static file and can no longer be cached by the browser, so it has to be re-downloaded every time.

It'd be better to take that times array out of the JavaScript file entirely, and make your PHP code produce an additional <script> tag in the page's header which just defines that variable. That way, the times can be generated dynamically while the rest of the JavaScript code remains static.

5 Comments

The JavaScript file will be re-downloaded every time anyway, so I'm not to worried about it being dynamic. And even if I were to do it your way, how would I take the 8:00|8:00 out of it?
When you're building it dynamically with PHP code, just don't put the 8:00 entry into it.
But how would I keep the rest of the file the same.
Put the times in a PHP array; that's easy to manipulate with PHP code. Either start with an empty array and then add only the entries you want, or start with the "full" array and remove the entries you don't want. Either array, once you have your PHP array with just the times you want, loop over that PHP array and print the brackets, quotes, time values, etc. that make up the JavaScript array.
Remember that JavaScript code is just a string as far as PHP is concerned. You can produce that "times[1]=" line with PHP echo statements.
0

If you want to only replace the occurance on this specific line you may want to use preg_replace() for this:

<?php
$filename = 'yourfile.js';
$content = file_get_contents( $filename );

$search = '"8:20am|8:20am"';
$pattern = '/^(\s*times\[1\]\s*=\s*\[.*)(' . 
           preg_quote($search) .
           ',?\s*)(.*\]\s*;?)$/m';

$content = preg_replace( $pattern, '\1\3', $content ); 

file_put_contents( $filename, $content );
?>

Result:

var daylist = document.checkout.checkoutDay
var timelist = document.checkout.times
var times = new Array()
times[1]=["8:00am|8:00am", "8:40am|8:40am", "9:00am|9:00am"]

The regular expression is built rather complex, to allow some flexibility in matching. it allows arbitrary whitespace (which would be valid to JS as well) and removes the comma, if the value is in the middle of the list. Note that the expression also produces a valid result when there is no comma. (when you want to remove the last element from the list)

Comments

0

Try fopen() method and get all the file content in a php variable then you can use str_replace() for find and replace the specific string and write to same js file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.