开发者

pleas help me with php trim function

trim function delete all more spaces but when try to save data in database it was save with more spaces why pleas help me i but down all code and database construction to help be

CREATE TABLE  `test`.`user` (
`id` INT( 11 ) NOT NULL AUTO_INCREME开发者_JAVA技巧NT PRIMARY KEY ,
`username` VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM ;

////////////////////////
<?php
    $conn = mysql_connect("localhost","root","");
    if(!$conn){
        die("there is problem in" .mysql_error());
    }
    $select_db = mysql_select_db("test",$conn);
    if(!$select_db){
        die("there is problem in" .mysql_error());
    }
?>
<?php
if(isset($_POST["submit"])){
    $fname = trim($_POST["text"]);
    $name = trim($fname);
    $insert = mysql_query("insert into user(username)values('{$name}')");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<form action="" method="post">
<input type="text" name="text" value=""/>
<input type="submit" name="submit" value="submit"/>
</form>
<body>
</body>
</html>


The trim function only removes leading and trailing spaces, not double spaces within the text. You can use preg_replace for that, e.g.:

$output = preg_replace('!\s+!', ' ', $input);

Source: php Replacing multiple spaces with a single space


Even simpler than using regex

$without_spaces = str_replace(" ", "", $_POST["text"]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜