How to check if “ ” exist?

Multi tool use
Multi tool use


How to check if “ ” exist?



When I print a variable, I get a blank result and when I inspect the element I see that  .


 



I tried to check if the variables is empty or equals this value:


ir( empty($string) || $string == " " || strpos($string, " ") || $string == " "){
//Do Something.
}



But the code inside that condition is not executed.



When I var_dump($string), I get:


var_dump($string)


string(2) " "



What should I do to check if the variable equals or contain that?





I would suggest to check for null or empty string using trim(): (!isset($string) || trim($string) === '')
– wp78de
Jul 1 at 6:00



(!isset($string) || trim($string) === '')





what is ir ??
– smith
Jul 1 at 7:02


ir





must be a typo: if
– wp78de
Jul 1 at 7:48


if





Still doesn't work
– Ashley
Jul 1 at 8:33





Thanks, The problem is resolved
– Ashley
Jul 1 at 9:30




2 Answers
2



First of all, People get tripped up on this move all the time...


strpos($string, " ")



If   is at the start of your string, then the evaluated result is 0 ("offset position") AND 0 is loosely compared to false in the way that you have crafted your conditional expression.


 


0


0


false



You need to explicitly check for false (strict check) from strpos() like this:


false


strpos()


if (empty($string) || strpos($string, " ") !== false || $string == " ") {
//Do Something.
}



You have a multibyte space evidenced by when you "highlight" the character with your cursor -- it only has a character length of one, but when you call var_dump() there is a byte count of 2.


var_dump()


2



trim() can't help you. ctype_space() can't help you. You need something that is multibyte aware.


trim()


ctype_space()



To allow the most inclusive match, I'll employ a regular expression that will search for all whitespace characters, invisible control characters, and unused code points.


if (empty($string) || preg_match("/^[pZpC]+$/u", $string)) {



This will check if the string is truly empty or is entirely composed of one or more of the aforementioned characters.



Here's a little demo: https://3v4l.org/u7eoK



(I don't really think this is a   issue, so I am leaving that out of my solution.)


 



Scroll down this resource: https://www.regular-expressions.info/unicode.html





I have just seen the answer, I didn't get a notification
– Ashley
2 days ago



The solution, If the utf-8 is used:


$a = str_replace("xC2xA0", '', $a);



If ASCII :


$a = str_replace("xA0", '', $a);



Then the $a is empty now and you could check it using if(empty($a))


if(empty($a))



The answer exists here: Does html_entity_decode replaces   also? If not how to replace it?





Typically empty() or isset() is called before any other processes are run on the variable. This is done to avoid generating Notices in the event that a SUPERGLOBAL key is not declared.
– mickmackusa
2 days ago


empty()


isset()


Notices





You mean that both can't be used to check the last result, Or I miss understood you?
– Ashley
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

rldKYP3,K9JoDKZe2GOr eh,49rwFEqXd 3ZCoe6iB2yG ESGWiG48,FjDkRsMjb,R
CSpwwBo62Sb5 RVzQvjNU85A4iUlU0X0 fsTVv0mE0LO5aI4YNZem3Rp,2EzLTwOgg9k9hf,YI6 H2z7i56,9ceCe dSEvNco

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask