susanBuck / e15-spring22

0 stars 0 forks source link

How to check for non-alphabets #6

Closed mgarimelHES closed 2 years ago

mgarimelHES commented 2 years ago

I am wondering if there is any way we could check for non-alphabet characters as shown in the notes for Palindrome (ex- "!racecar!")?

One way that I am thinking about leveraging the built-in function like 'ctype_alpha', not sure if there are better options exist?

Thanks

dauger27 commented 2 years ago

The solution I found that worked well for me was using Regular Expressions to find the non-alphabetic characters and remove them.

susanBuck commented 2 years ago

There are several ways to go about this.

As @dauger27 suggested, you could work with a regular expression combined with one of php's built in reg exp. functions, e.g. preg_filter.

Or, as you noted, you could use ctype_alpha, but you would want to combine this some way of filtering just the letters. This could be as simple as a loop that goes through each character in the string and disregards any characters that don't pass ctype_alpha.

Or, a similar loop/filter approach could be accomplished with array_filter.

mgarimelHES commented 2 years ago

Thanks @dauger27 and @susanBuck , I will try the suggested options.

Appreciate your help

mgarimelHES commented 2 years ago

Thanks, able to do it with ctype_char.