[PHP]文字列すべてスペースかどうか判定

<?php

function isStringOnlySpaces($str) {
    // Use a regular expression to check if the string contains only spaces
    return preg_match('/^[\s\x{3000}]*$/u', $str);
}

// Test cases
$string1 = "   ";  // Only contains spaces (single-byte)
$string2 = "  ";  // Only contains spaces (double-byte)
$string3 = "Hello, world!";  // Contains non-space characters

if (isStringOnlySpaces($string1)) {
    echo "String 1 contains only spaces.\n";
} else {
    echo "String 1 does not contain only spaces.\n";
}

if (isStringOnlySpaces($string2)) {
    echo "String 2 contains only spaces.\n";
} else {
    echo "String 2 does not contain only spaces.\n";
}

if (isStringOnlySpaces($string3)) {
    echo "String 3 contains only spaces.\n";
} else {
    echo "String 3 does not contain only spaces.\n";
}
?>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です