Insert string with strtoupper

Asked

Viewed 59 times

2

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

Exemplo

The problem is that it converts everything that is in the shortcode to uppercase, and I don’t want that. 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);

I replaced strtoupper for strtolower to see if this is really the part that made the conversion and it is. How do I call the string $Chord by substituting b for but without changing the letter box?

Thank you!

  • remove the 2 conditions function $chordPretty = (strlen($Chord)>1&&substr($Chord, 1,1)==='b')? substr($Chord, 0,1). ' : $Chord;

2 answers

2


Try it like 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 keeps the original value.

  • It worked... He removed the upper, but there is another line that is applying strtolower: $chord = strtolower($atts[0]); I can set $Chord = $atts ?

  • Yes, to maintain the state of the string, remove the function and leave only $Chord = $atts[0];

  • Success! : ) Thanks!

2

If the idea is to replace everything could simply use the str_replace and the strtoupper to keep everything in check, 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;

Upshot:

Ab => A♭
F# => F#
C# => C#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.