Jump to content
#StayAtHome ×
ajoo

string checks

Recommended Posts

Hi, 

I wish to check for a condition that a string received is not white space or false or null but it should allow the string '0'.

This must be a very simple problem but it is definitely quite confusing.

Thanks loads.

Share this post


Link to post
Share on other sites

Hi, 

The following seems to work but still allows for a non string 0.

(isset($str) && $str !==false && $str !==''){
.
.
.

}

Maybe I need to check for an is_string as well. I was hoping to find something simpler if it exists.

Thanks !

Share this post


Link to post
Share on other sites

If I understand, you are really using the wrong approach. You really want to use preg_match and a regular expression. I am not a regexp expert so you should ask tor the expression in the regexp forum.

Share this post


Link to post
Share on other sites

If it's a string then it won't be ===false. Because it's a string. Same for ===null.

Maybe what you're looking for is

isset($_POST["whatever"]) && trim($_POST["whatever"]) != ""

 

Share this post


Link to post
Share on other sites

Hi !

@ gw1500se :  While it's complicated I don't think it would require a regex since it's also basic. 

@requinix : Since this is an input received via ajax, someone can try and doctor the input to try and break the code. Hence I had the == false as well. And since a string won't be equal to false, I am thinking that it may be necessary to check that the input received is indeed a string. Wouldn't that be so ? Would you still go ahead with your code that you pasted above?

Thanks ! 

 

 

Share this post


Link to post
Share on other sites
27 minutes ago, ajoo said:

Since this is an input received via ajax, someone can try and doctor the input to try and break the code. Hence I had the == false as well. And since a string won't be equal to false, I am thinking that it may be necessary to check that the input received is indeed a string. Wouldn't that be so ? Would you still go ahead with your code that you pasted above?

It will be either a string or an array. If you want to protect against that, a simple is_string() will suffice.

Share this post


Link to post
Share on other sites

hmm, 

So I can finally use 

isset($_POST["whatever"]) && is_string($_POST["whatever"]) && trim($_POST["whatever"]) != ""

because, as you said, if it's a string, it won't be false or null.

Thanks.

 

 

Share this post


Link to post
Share on other sites

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.