Jump to content

Checking a string for actual letters?


galvin

Recommended Posts

Say I have a string (stored in a variable we'll call $mystring) that could be something like this...

 

"cat / dog / monkey / / / / / / / /"

 

Or this string could also have no real data in it, in which case it would look like...

 

"/ / / / / / / / / /"

 

QUESTION:  What is the easiest/most efficient way to run a check on that string to see if there are any non-slash characters in it.  In plain english, the check would say, "Are there any letters in this string, or is it only spaces and slashes?"

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/
Share on other sites

a feel a regular expression expert will answer this, i have a convoluted solution that would use explode() to split string to an array using the slash as the delimiter, then do a ctype_alpha() check on each element of array.

 

 

 

I so need to learn reg exp...

I'm not great with regex, but this seems to work if there are any characters other that slashes and/or spaces . . .

 

if( preg_match('~[^/\s]~', $string) ) {
   echo "NOT just slashes and/or spaces.";
} else {
   echo 'ONLY slashes and/or spaces';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.