Insert string with strtoupper

2

Good morning! I am using a code in WP to display musical notes. It basically takes text from a shortcode "chord" and displays it in the line above. It also converts the b (lowercase) to ♭. Example:

The problem is that it converts everything in the shortcode to upper case, and I do not want it. I just want it to display what is typed. In his code I found the following line:

        $chordPretty = (strlen($chord)>1&&substr($chord, 1,1)==='b')? strtoupper(substr($chord, 0,1)).'♭' : strtoupper($chord);

Replace strtoupper with strtolower to see if it was really that part that did the conversion and is in fact . How do I call the string $ chord by replacing b for ♭ but without changing the letter box?

Thank you!

    
asked by anonymous 18.09.2017 / 14:55

2 answers

2

Try this:

$chordPretty = (strlen($chord)>1&&substr($chord, 1,1)==='b')? substr($chord, 0,1).'♭' : $chord;

If the string has more than 1 character, and if the second character is 'b', it returns the first character of the string + ♭. Otherwise, it retains the original value.

    
18.09.2017 / 14:59
2

If the idea is to replace everything, you could simply use str_replace and strtoupper to leave everything in the upper case, if that's what you want.

$listaDeSubstituição = [
    'b' => '♭'
];

$chord = strtr($chord, $listaDeSubstituição)

// Se quiser mudar "tudo" para maiúsculo:
// $chord = strtoupper($chord);

echo $chord;

Result:

Ab => A♭
F# => F#
C# => C#
    
18.09.2017 / 15:22