Check for spaces php

How to Check if String Contains Whitespace in PHP?

Are you looking for example of How to Check if String Contains Whitespace in PHP. Here you will learn If Name Value Contains Space In Php With Code Examples. let’s discuss about PHP in ctype_space() Function Example. We will use Check Blank Space Using PHP. So, let’s follow few step to create example of How to check if string contains only a spaces in PHP.

A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.

Programs illustrate the ctype_space() function, taking a single string. Takes an array of string and checking for whitespace using ctype_space() function. Takes an example ctype_space() function how to work string in both single quotes ‘ ‘ and double quotes ” ” symbol.

// PHP program to check given string is

// whitespace character or not

$string = «/n/t/r»;

if (ctype_space($string)) <

echo «Yes \n»;

>

else <

echo «No \n»;

>

?>

// PHP program to check given string is

// whitespace character or not

$strings = array(‘\n\y\t\x\u\o’, «\ngfg\n», «\t»);

// Checking above given strings

//by used of ctype_space()function .

foreach ($strings as $testcase) <

if (ctype_space($testcase)) <

echo «Yes \n»;

> else <

echo «No \n»;

>

>

?>

// PHP program to check given string is

// whitespace character or not

$strings = array(‘\n’, «\n», ‘\t’, «\t»);

// Checking above given strings

// by used of ctype_space()function .

foreach ($strings as $testcase) <

if (ctype_space($testcase)) <

echo «Yes \n»;

> else <

echo «No \n»;

>

>

?>

✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.

You might also like.

Источник

ctype_space

Checks if all of the characters in the provided string , text , creates whitespace.

Parameters

Note:

If an int between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative values have 256 added in order to allow characters in the Extended ASCII range). Any other integer is interpreted as a string containing the decimal digits of the integer.

As of PHP 8.1.0, passing a non-string argument is deprecated. In the future, the argument will be interpreted as a string instead of an ASCII codepoint. Depending on the intended behavior, the argument should either be cast to string or an explicit call to chr() should be made.

Читайте также:  Расположить блок снизу css

Return Values

Returns true if every character in text creates some sort of white space, false otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters. When called with an empty string the result will always be false .

Examples

Example #1 A ctype_space() example

$strings = array(
‘string1’ => «\n\r\t» ,
‘string2’ => «\narf12» ,
‘string3’ => ‘\n\r\t’ // note the single quotes
);
foreach ( $strings as $name => $testcase ) if ( ctype_space ( $testcase )) echo «The string ‘ $name ‘ consists of whitespace characters only.\n» ;
> else echo «The string ‘ $name ‘ contains non-whitespace characters.\n» ;
>
>
?>

The above example will output:

The string 'string1' consists of whitespace characters only. The string 'string2' contains non-whitespace characters. The string 'string3' contains non-whitespace characters.

See Also

  • ctype_cntrl() — Check for control character(s)
  • ctype_graph() — Check for any printable character(s) except space
  • ctype_punct() — Check for any printable character which is not whitespace or an alphanumeric character

User Contributed Notes 2 notes

A function I wrote last night was fairly flexible in terms of detecting whitespace, and even took into account the pesky non-breaking spaces / zero-width spaces further up the Unicode alphabet.

The benefit here was being able to isolate and identify specific Unicode indices based on their subrange.

// Returns TRUE if the ASCII value of $string matches a registered whitespace character.
// * This includes non-breaking spaces, zero-width spaces, and any unicode values below 32.
// * $string: Character to identify. If string extends past one character, the value
// is truncated and only the initial character is examined.
function is_whitespace ( $string ) <
// Return FALSE if passed an empty string.
if( $string == «» ) return FALSE ;

// Additional Characters
switch( $char ) <
case 160 : // Non-Breaking Space
case 8287 : // Medium Mathematical Space
return TRUE ;
break;
>
return FALSE ;
>
?>

thanks to gardnerjohng, but a had some problems with non-breaking spaces in this function.

I added 2 more cases for this:
case 0xC2
case 0xA0

After this modification non-breaking spaces in my test code were successfully detected.

Источник

5 Effective Ways to Check If a String Contains Only Whitespace in PHP

Learn how to check if a string contains only whitespace in PHP with these 5 effective methods. Use ctype_space() function, regular expressions, string methods, and other functions to ensure your code handles whitespace correctly and avoid errors.

  • Using ctype_space() function
  • Using Regular Expressions
  • Using String Method
  • Using strlen(trim()) Function
  • Using preg_match() Function to Check for Letters and Spaces
  • Other quick code examples to check if a string contains only whitespace in PHP
  • Conclusion
  • How do you check if a string is only spaces PHP?
  • How do you check if a string has only spaces?
  • How do I echo whitespace in PHP?
  • How do I check if a string contains something in PHP?
Читайте также:  Cpp const class members

If you are working with strings in PHP, there may be times when you need to check whether a string contains only whitespace characters or not. In this article, we will discuss different ways to check if a string contains only whitespace in PHP.

Using ctype_space() function

One of the most straightforward methods to check if a string contains only whitespace in PHP is by using the ctype_space() function. This function checks if each character in a string is a whitespace character. It returns true if all characters are whitespace, and false otherwise.

Here is an example code snippet that demonstrates the usage of ctype_space() function:

$string = " \t\n"; if (ctype_space($string))  echo "The string contains only whitespace."; > else  echo "The string contains non-whitespace characters."; > 

The above code will output “The string contains only whitespace.” as the input string contains only whitespace characters.

It is worth noting that the ctype_space() function also includes non-breaking spaces, zero-width spaces, and any other whitespace characters.

Using Regular Expressions

Another method to check if a string contains only whitespace characters is by using regular expressions. Regular expressions are powerful tools that allow you to match patterns in strings.

The regular expression ‘/^\s+$/ ‘ matches strings containing only whitespace characters or nothing at all. Here’s an example code snippet that demonstrates the usage of regular expressions to check for whitespace characters:

$string = " "; if (preg_match('/^\s+$/', $string))  echo "The string contains only whitespace."; > else  echo "The string contains non-whitespace characters."; > 

The above code will output “The string contains only whitespace.” as the input string contains only whitespace characters.

Using the preg_match() function with other regular expressions can check for specific characters or patterns in a string.

Using String Method

The str.isspace() method is another way to check if a string contains only whitespace characters. It checks if a string contains only whitespace characters and returns true if there is at least one character in the string and all characters are whitespace.

Here is an example code snippet that demonstrates the usage of str.isspace() method:

$string = " "; if (str.isspace($string))  echo "The string contains only whitespace."; > else  echo "The string contains non-whitespace characters."; > 

The above code will output “The string contains only whitespace.” as the input string contains only whitespace characters.

It is recommended to use the trim() function to remove whitespace from the beginning and end of a string before checking for whitespace characters.

Using strlen(trim()) Function

The strlen(trim()) function is another method to check if a string is empty or contains only whitespace. It returns 0 if the string is empty or contains only whitespace.

Here is an example code snippet that demonstrates the usage of strlen(trim()) function:

$string = " "; if (strlen(trim($string)) === 0)  echo "The string contains only whitespace."; > else  echo "The string contains non-whitespace characters."; > 

The above code will output “The string contains only whitespace.” as the input string contains only whitespace characters.

It is worth noting that the empty() function can also be used to check if a string is empty, but note that it also considers ‘0’ and ‘false’ as empty.

Using preg_match() Function to Check for Letters and Spaces

The preg_match() function can also be used to check if a string contains only letters and spaces. The regular expression «/^[a-zA-Z\s]+$/ » returns true if the string contains only letters and spaces, and false otherwise.

Here is an example code snippet that demonstrates the usage of preg_match() function to check for letters and spaces:

$string = "Hello World"; if (preg_match('/^[a-zA-Z\s]+$/', $string))  echo "The string contains only letters and spaces."; > else  echo "The string contains non-letter or non-space characters."; > 

The above code will output “The string contains only letters and spaces.” as the input string contains only letters and spaces.

The preg_match() function can be used with other regular expressions to check for specific characters or patterns in a string.

Other quick code examples to check if a string contains only whitespace in PHP

In Php , for example, if name value contains space in php code sample

if ($str == trim($str) && strpos($str, ' ') !== false)

In Php case in point, check if string contains only whitespace php code example

ctype_space(" ") //returns truectype_space(" a ") //returns false

Conclusion

In this article, we have discussed different ways to check if a string contains only whitespace in PHP. The ctype_space() function, regular expressions, string methods , and other functions can be used.

It is recommended to use the trim() function before checking for whitespace characters and using the appropriate functions for specific needs. By using these methods, programmers can ensure that their code handles whitespace correctly and avoids errors.

Источник

Оцените статью