galvin Posted May 2, 2011 Share Posted May 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/ Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/#findComment-1209263 Share on other sites More sharing options...
galvin Posted May 2, 2011 Author Share Posted May 2, 2011 I hear ya. I started using explode() and then was planning to loop through each one but there HAS to be an easier/more efficient way so I figured I'd run it by the geniuses on this website I really need to lear reg exp too Quote Link to comment https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/#findComment-1209264 Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 yeah there will be one that checks against only ' slash or space' , thats all you will need. Quote Link to comment https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/#findComment-1209270 Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/235318-checking-a-string-for-actual-letters/#findComment-1209281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.