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?
If you want to only replace the occurance on this specific line you may want to use
preg_replace()for this:Result:
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)