Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:54:36+00:00 2026-06-02T22:54:36+00:00

Using PHP, I want to be able to open a JavaScript file and take

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:54:37+00:00Added an answer on June 2, 2026 at 10:54 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to open and read an XML file using php, but I wont
I want the following functionality using php I have a csv file. Each file
I want to trigger javascript alert using PHP. Is it possible I want to
Please could someone help with the following: Using PHP I want to be able
Using PHP and MySQL, I want to select only 6 rows from table which
Using PHP, I want to convert UNIX timestamps to date strings similar to this:
I actually want to generate random password using PHP uniqid() function $randomPassword = uniqid();
Using PHP, I am trying to delete a record, but I want to check
I want to create sub-domains using PHP on the fly. Suppose a user registers
I want to create a cookie using php,an also i need to read it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.