PHP - Detect Whitespace Between Strings

PHP check if string contains space between words (not at beginning or end)

You can verify that the trimmed string is equal to the original string and then use strpos or str_contains to find a space.

// PHP < 8
if ($str == trim($str) && strpos($str, ' ') !== false) {
echo 'has spaces, but not at beginning or end';
}

// PHP 8+
if ($str == trim($str) && str_contains($str, ' ')) {
echo 'has spaces, but not at beginning or end';
}

Some more info if you're interested

If you use strpos(), in this case, you don't have to use the strict comparison to false that's usually necessary when checking for a substring that way. That comparison is usually needed because if the string starts with the substring, strpos() will return 0, which evaluates as false.

Here it is impossible for strpos() to return 0, because the initial comparison

$str == trim($str) eliminates the possibility that the string starts with a space, so you can also use this if you like:

if ($str == trim($str) && strpos($str, ' ')) { ...

If you want to use a regular expression, you can use this to check specifically for space characters:

if (preg_match('/^[^ ].* .*[^ ]$/', $str) { ...

Or this to check for any whitespace characters:

if (preg_match('/^\S.*\s.*\S$/', $str) { ...

I did some simple testing (just timing repeated execution of this code fragment) and the trim/strpos solution was about twice as fast as the preg_match solution, but I'm no regex master, so it's certainly possible that the expression could be optimized to improve the performance.

PHP - detect whitespace between strings

Use preg_match as suggested by Josh:

<?php

$foo = 'Bob Williams';
$bar = 'SamSpade';
$baz = "Bob\t\t\tWilliams";

var_dump(preg_match('/\s/',$foo));
var_dump(preg_match('/\s/',$bar));
var_dump(preg_match('/\s/',$baz));

Ouputs:

int(1)
int(0)
int(1)

PHP - make sure string has no whitespace

if ( preg_match('/\s/',$username) ) ....

How do I strip all spaces out of a string in PHP?

Do you just mean spaces or all whitespace?

For just spaces, use str_replace:

$string = str_replace(' ', '', $string);

For all whitespace (including tabs and line ends), use preg_replace:

$string = preg_replace('/\s+/', '', $string);

(From here).

Check if string is just white space?

This will be the fastest way:

$str = '      ';
if (ctype_space($str)) {

}

Returns false on empty string because empty is not white-space. If you need to include an empty string, you can add || $str == '' This will still result in faster execution than regex or trim.

ctype_space

PHP: How do I test for whitespace at the beginning and end of a string?

$string = <your string>;
$ns_string = trim($string);
$spaces_present = ($ns_string == $string) ? false : true;

in shorter notation

$space_present = ($string != trim($string));

Remove the space between two words in PHP

'PaneerPakodaDish' should be the desired output.

$string = 'Paneer Pakoda dish';
$s = ucfirst($string);
$bar = ucwords(strtolower($s));
echo $data = preg_replace('/\s+/', '', $bar);

It will give you the exact output 'PaneerPakodaDish' where character "D" will also be in capital.



Related Topics



Leave a reply



Submit