PHP检查它是否包含字母数字或空格以外的任何内容

PHP检查它是否包含字母数字或空格以外的任何内容,php,string,Php,String,我有一个发送字符串的表单。就这么简单: <form action="test2.php" method="POST"> String: <input type="text" name="string" /> <br /><input type="submit" value="Send >" /> </form> 字符串: 下面是test2.php: <?php $string = $_POST['string'];

我有一个发送字符串的表单。就这么简单:

<form action="test2.php" method="POST">
String: <input type="text" name="string" />
<br /><input type="submit" value="Send >" />
</form>

字符串:

下面是test2.php:

<?php
$string = $_POST['string'];
preg_replace("/[^0-9a-zA-Z ]/", "", $string {
echo "You can't have any symbols in your username.";
} else {
echo "Nice string!";
}
?>

您的语法完全无效。你想要这样的东西:

<?php
$string = $_POST['string'];
if ( preg_match("/[^0-9a-zA-Z ]/", $string) ) {
    echo "You can't have any symbols in your username.";
} else {
    echo "Nice string!";
}
?>

我发现了一个致命错误。将其标记为已接受,然后单击复选标记。