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 8591359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:30:38+00:00 2026-06-11T23:30:38+00:00

I’m trying to rewrite a function in javascript to php but it is not

  • 0

I’m trying to rewrite a function in javascript to php but it is not working. Maybe someone here can tell me where I am going wrong? Thank you in advance.

Javascript… (working function)

var match;
var chords = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C'];
var chords2 = ['C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C'];
var chordRegex = /(?:C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B)/g;
var rec = /\{(.*?)\}/g; // gets all info between { and }

function transposeUp(){
    var html = $('.yui-editor-editable').contents().find('body'); //gets the contents
    var matches = $(html).html().match(rec);/////Gets all matches within the html
    var text = $(html).html(); // html of the html var
    for (var i = 0; i < matches.length; i++) { /////foreach match do
        ///// initializes variables /////
        var currentChord = String(matches[i]); //// sets current chord 
        currentChord = currentChord.substring(currentChord.indexOf("{") + 1, currentChord.indexOf("}")); //sets current chord cont.
        var output = "";
        var parts = currentChord.split(chordRegex); // splits currentChord into parts in the chordRegex
        var index = 0;
        /////////////////////////////////
        while (match = chordRegex.exec(currentChord)) { // while the match is equal to the currentChord do
            var chordIndex = chords2.indexOf(match[0]); // find the position of the matched chord within the chords2 array
            output += parts[index++] + chords[chordIndex + 1]; // build the output
        }
        output += parts[index];
        text = text.replace(matches[i], '{'+output+'}'); //replace the text with the new chords
    }
    $(html).html(text); // set the html value as the new text
}

PHP…

function transposeUp($songchart){
$chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C');
$chords2 = array('C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C');
$chordRegex = "/(?:C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B)/";

  $matches = preg_match_all('#{.*(.*)}#U', $songchart, $chords);
  $chord = $chords[1];
  foreach($chord as $key => $value){ 
    $output = '';
    $parts = preg_split($chordRegex, $value);
    $index = 0;
    while(preg_match($chordRegex, $value, $note)){
        $chordIndex = strpos($chords2, $note[0]);
        $output .= $parts[$index++] . $chords1[$chordIndex + 1];
    }
    $output .= $parts[$index];
    $songchart = preg_replace($value, $output, $songchart);
  }
return($songchart);
}
  • 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-11T23:30:39+00:00Added an answer on June 11, 2026 at 11:30 pm
    $matches = preg_match_all('#{.*(.*)}#U', $songchart, $chords);
    

    should be:

    $matches = preg_match_all('#\{(.*)\}#U', $songchart, $chords);
    

    In your regexp, the first .* was consuming everything inside the braces, so the second one got nothing.

    I don’t understand the while loop. $value never changes, so how will the loop ever terminate?

    $chordIndex = strpos($chords2, $note[0]);
    

    should be:

    $chordIndex = array_search($chords2, $note[0]);
    

    since strpos is for searching in strings, not arrays.

    $songchart = preg_replace($value, $output, $songchart);
    

    should be:

    $songchart = str_replace($value, $output, $songchart);
    

    since $value is not a regular expression.

    I’m not sure of everything this is doing, but I suspect it could be simplified by using preg_replace_callback, or maybe even a single str_replace with arrays for the search and replacement arguments.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.