Remove characters from a string

I'm writing my first object oriented script that spells triads and chords derived from the major, harmonic minor or jazz minor.

I need to remove the characters  ♭ ♯ 𝄪  𝄫 from a note.

So I need to remove the ♯ from D♯ and return a D.

What is the best way to do this?
Who is Participating?
Chris StanyonConnect With a Mentor Commented:
Probably several ways to do it. Loops / Regex / Trimming.

Maybe the simplest is to not 'remove' specific characters, but to only leave specific character, either base on a collection of 'alloweds' or even easier - just return the first character:

var note = "D♯";
var baseNote = note[0];

Open in new window

Julian HansenCommented:
I might have misunderstood the requirement but can't you do a str_replace on the char you want to replace? If you need to remove multiple chars then preg_replace
$source = "D♯A♭B𝄪C♯EF𝄫G";
$result = preg_replace('[♭|♯|𝄪|𝄫]', '', $source);
echo $result;

Open in new window

$str = preg_replace("/[^A-Za-z0-9?! ]/","",$str);
this will allow only letters numbers and spaces in your string
Chris StanyonCommented:
Oops - for some reason, I read this as a C# question - not a PHP one. Here's the PHP version of it:

$note = "D♯";
$baseNote = $note[0];

Open in new window

burnedfacelessAuthor Commented:
It's cool man I was in C# before I was suspended when marijuana was found in my dorm. I knew what you were saying I was smoking more marijuana instead of coding.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.