If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Checking for characters in PHP variable

Started by AliceFowell89, Jul 20, 2022, 09:51 AM

Previous topic - Next topic

AliceFowell89Topic starter

Forum users, can you please advise me on how to determine if a variable, which has a certain value, contains a character other than a number? Which function would be suitable for this purpose?
  •  


organictextiles

To ensure that there are no characters other than numbers in an integer variable, you can use typecasting to convert it to a positive integer using (int) $x; as described in http://php.net/manual/en/language.types.type-juggling.php.

If you need to check for the presence of a number as stated in your question, take note that if the information is from a form, it will always be treated as a string and PHP may implicitly perform a type conversion unless otherwise specified. However, if the data is exchanged between scripts internally, different types could potentially be used.
  •  

MARK PETERSON

When it comes to finding the precise instance of a word, regular expressions can be more effective despite the faster speed of the strpos() and stripos() functions. These built-in PHP functions may present problems for accurately finding the exact occurrence of a word. In that case, regular expressions are the preferable choice.

To mark a word boundary in a regular expression template, you can use the "b" expression. This method allows the preg_match() function to locate exact matches, return FALSE for partial matches, and achieve more accurate results. Here is an example:

$the_string = 'Photosynthesis and risky are long words.';
$the_word = 'synthesis';

// Output - The word "synthesis" has an exact match in the given string. [FALSE positive]
if (preg_match('/synthesis/', $the_string)) {
  echo 'The word 'synthesis' has an exact match on this line. [FALSE positive]';
}

// Output - The word "synthesis" has an exact match in the given string. [FALSE positive]
if (strpos($the_string, $the_word)) {
  echo 'The word 'synthesis' has an exact match on this line. [FALSE positive]';
}

// Output - The word "synthesis" does not have an exact match in this string. [Expected Result]
if (preg_match('/bsynthesisb/', $the_string)) {
  echo 'The word 'synthesis' has an exact match on this line. [FALSE positive]';
} else {
  echo 'The word 'synthesis' does not have an exact match in this string. [Expected Result]';
}
  •  

samarsing

You gotta understand that there's no magic function that's gonna do all the work for you. You're gonna have to put in some elbow grease, got it? Now, there are a couple of ways you can approach this. I'm gonna tell you two methods, and you can take your pick.

Method one: Regular Expressions. Now, I know what you're thinking, "Oh great, another one of those scary-looking things I don't understand." Well, tough luck, 'cause they're powerful tools, and you're gonna need to learn 'em sometime. Here's how you can use 'em for your little problem:

import re

def has_non_number(input_string):
    return bool(re.search(r'\D', input_string))

# Usage
print(has_non_number("12345"))  # False
print(has_non_number("123abc"))  # True

See that \D thing? That's a regular expression that matches any non-digit character. The re.search function finds the first match, and bool turns that into a boolean value. Easy peasy, right?

Method two: String methods. If you're too chicken to use regular expressions, you can use string methods instead. It's a bit more verbose, but hey, at least you won't have to learn anything new, right?

def has_non_number(input_string):
    for char in input_string:
        if not char.isdigit():
            return True
    return False

# Usage
print(has_non_number("12345"))  # False
print(has_non_number("123abc"))  # True

Here, we're just looping through each character in the string and checking if it's a digit using the isdigit method. If we find one that's not, we return True. If we get through the whole string without finding any, we return False.

Now, you might be thinking, "But what if I want to check for whitespace or other non-digit, non-alphanumeric characters?" Well, tough luck, 'cause I'm not your personal coding assistant. You figure that one out yourself.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...