Joomla使用JInput检查空字符串 [英] Joomla check for empty string with JInput

查看:102
本文介绍了Joomla使用JInput检查空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循此指南来清理输入内容,我想知道是否此内容包含空字符串?

Following this guide to sanitize my inputs, I'm wondering if an empty string is covered with this?

$jinput = JFactory::getApplication()->input;
$this->name = $jinput->get('name', '', 'STRING');

通常没有Joomla,我也会检查一个空字符串.像这样:

Typically without Joomla I'd be checking for an empty string as well. Something like:

if (!empty($_POST['name']))

查看JInput get方法,我发现它检查它是否为isset:

Looking at the JInput get method I see that it checks if it is isset:

public function get($name, $default = null, $filter = 'cmd')
{
    if (isset($this->data[$name]))
    {
        return $this->filter->clean($this->data[$name], $filter);
    }

    return $default;
}

不一样,因为isset仅检查null.但是,这是使用get方法的默认值.因此,如果我为第二个参数指定一个空字符串,我在这里覆盖了吗?

Not the same thing, as isset will only check for null. However that is the default value for using the get method. So if I specify an empty string for the second parameter am I covered here?

$this->name = $jinput->get('name', '', 'STRING');

推荐答案

由Joomla来决定您的空字符串是否为有效值不是由您决定的.他们必须使用isset(),因为如果他们使用empty()并且您返回了通常期望的'0',则Joomla将返回默认值而不是该'0'.

It's not up to Joomla to decide whether your empty string is valid value or not. They have to use isset(), because if they would use empty() and you return '0' which you would expect as normal, Joomla would return default value instead of that '0'.

因此,他们只使用isset()来检查是否设置了变量是完全正常的,并且由您决定接受什么值.

So it's completely normal that they just use isset() to check if variable is set, and it's up to you to decide what values you accept.

如果未设置该值,并且将空字符串''设置为第二个参数,则会返回一个空字符串.

If the value isn't set, and you set as the second parameter empty string '', you'll get an empty string returned.

在您的示例中,将返回一个空字符串,这是预期的行为.

In your example an empty string would be returned, which is expected behaviour.

这篇关于Joomla使用JInput检查空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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