$ _POST将空格添加到变量值 [英] $_POST adding whitespace to variable value

查看:49
本文介绍了$ _POST将空格添加到变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的POST表单,包含两种类型的输入:文本输入和

textarea。表单从mysql数据库下载当前设置。

用户可以通过修改文本并单击

标准提交来更新信息。按钮。


主要问题:


我的问题是发送到formhandler的信息显然是

有一些添加了一些空格。如果我只是在POST

变量上使用trim(),它就会使正则表达式失败。如果我将POST变量转换为单个

变量并使用trim(),它就可以解决问题。


我可以转换并修剪每个变量,但是使用foreach()修整整个POST数组会更加清晰。另外,我宁愿

了解这个问题。换句话说:


此代码有效:

$ zip = trim($ _ POST [''zip'']);

if(preg_match(" / ^ [\d] {5} /",$ zip)){

$ update。=" zip =''$ zip''," ;;

} else {

error_alert('''邮政编码必须是5位数字,没有其他字符。'') ;

}


此代码抛出正则表达式拒绝:


$ _POST [''zip''] = trim($ _ POST [''zip'']);

if(preg_match(" / ^ [\d] {5} /" ;, $ _Post [''zip'' ])){

$ zip = $ _POST [''zip''];

$ update。=" zip =''$ zip''," ;;

} else {

error_alert('''邮政编码必须是5位数字,没有其他字符。'') ;

}


次要问题


当然,我真正的问题是空白是如何进入的第一个

的地方。或者更准确地说,为什么正则表达式会拒绝来自

数据库的信息。在$ zip的情况下,字段为VARCHAR(5)。


这里是表单条目:

$ zip = $ row [''zip''];

.. 。

< div class =" tr">

< div class =" tdfl">

邮政编码( 5位数):

< / div>

< div class =" tdfr">

< input type =" ;文本"名称= QUOT;拉链"大小= QUOT; 5英寸MAXLENGTH = QUOT; 5英寸value ="<?php

if(isset($ _ POST [''submit''])){

echo $ _POST [''zip''] ;

}否则{

echo $ zip;

}?>

" />

< / div>

< / div>


有谁知道为什么会这样?我可以解决它(有很多

的意大利面条代码)但是对于过渡的控制如此之少或

控制转换是令人不安的:数据库 - >表格输入 - >后变量 - >正则表达式。


TIA :)

-

Mason Barge

I have a standard POST form consisting of two types of input: text input and
textarea. The form downloads current settings from a mysql database. The
user can update the information by modifying the text and clicking a
standard "submit" button.

MAIN PROBLEM:

My problem is that the information transmitted to the formhandler apparently
has some sort of whitespace added to it. If I simply use trim() on the POST
variable, it fails the regex. If I convert to POST variable to a single
variable and use trim(), it solves the problem.

I can just convert and trim every variable, but it would be a lot cleaner to
trim the entire POST array with a foreach(). Plus, I''d much rather
understand the problem. In other words:

This code works:
$zip=trim($_POST[''zip'']);
if (preg_match("/^[\d]{5}/", $zip)) {
$update.=" zip=''$zip'',";
} else {
error_alert(''Zip code must be exactly 5 digits with no other characters. '');
}

This code throws a regex rejection:

$_POST[''zip'']=trim($_POST[''zip'']);
if (preg_match("/^[\d]{5}/", $_Post[''zip''])) {
$zip = $_POST[''zip''];
$update.=" zip=''$zip'',";
} else {
error_alert(''Zip code must be exactly 5 digits with no other characters. '');
}

SECONDARY PROBLEM

Of course, my real problem is how the whitespace gets in there in the first
place. Or more accurately, why the regex would reject information from the
database. In the case of "$zip", the field is VARCHAR(5).

Here''s the form entry:
$zip=$row[''zip''];
.. . .
<div class="tr">
<div class="tdfl">
ZIP code (5 digit):
</div>
<div class="tdfr">
<input type="text" name="zip" size="5" maxlength="5" value="<?php
if(isset($_POST[''submit''])) {
echo $_POST[''zip''];
} else {
echo $zip;
} ?>
" />
</div>
</div>

Does anyone have an idea why this happens? I can work around it (with a lot
of spaghetti code) but it''s unsettling to have so little understanding or
control of the transition: database->form input->Post variable->regex.

TIA :)
--
Mason Barge

推荐答案

zip = trim(
zip=trim(


_POST [''zip'']);

if(preg_match(" / ^ [\d] {5} /",
_POST[''zip'']);
if (preg_match("/^[\d]{5}/",


zip)){
zip)) {


这篇关于$ _POST将空格添加到变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆