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
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...

Link to comment
Share on other sites

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  :-[

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.