I've have a problem with preg_replace()
using arrays.
Basically, I'd like to transpose this string:
$string = "Dm F Bb F Am";
To
$New_string = "F#m D D A C#m";
Here is what I do:
$Find = Array("/Dm/", "/F/", "/Bb/", "/Am/", "/Bbm/", "/A/", "/C/");
$Replace = Array('F#m', 'A', 'D', 'C#m', 'Dm', 'C#', 'E');
$New_string = preg_replace($Find, $Replace, $string);
But I get this result instead:
E##m E# D E# E#m
Problem is that every match is replaced by the following, something like this happens (example for E##m):
Dm -> F#m -> A#m -> C##m -> E##m
Is there any solution to simply change Dm
to F#m
, F
to A
, etc ?
You could use strtr():
<?php
$string = "Dm F Bb F Am";
$Find = Array('Dm', 'F', 'Bb', 'Am', 'Bbm', 'A', 'C');
$Replace = Array('F#m', 'A', 'D', 'C#m', 'Dm', 'C#', 'E');
$New_string = strtr($string, array_combine($Find, $Replace));
echo $New_string;
// F#m A D A C#m