首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP/HTML文本输入中,如何剥离不希望提交到数据库的某些部分?

在PHP/HTML文本输入中,如何剥离不希望提交到数据库的某些部分?
EN

Stack Overflow用户
提问于 2013-01-31 01:29:07
回答 3查看 66关注 0票数 0

当人们在我的网站上编辑他们的帐户时,他们感到困惑,认为他们必须键入所有的蒸汽配置文件链接,例如http://steamcommunity.com/id/######,而他们只需在文本字段中键入#######,那么如果他们搞砸了,我如何从他们的输入中删除除#####部件之外的所有内容?

EN

回答 3

Stack Overflow用户

发布于 2013-01-31 01:30:41

这样如何:

代码语言:javascript
复制
$lastPart = array_pop(explode('/', $_SERVER['REQUEST_URI']));

这将检索REQUEST_URI的最后一个元素。

票数 0
EN

Stack Overflow用户

发布于 2013-01-31 01:31:23

str_replace("http://steamcommunity.com/id/","",$input);是一种方法。

一种更健壮的方式是:

preg_replace("@^.*id/@","",$input);

票数 0
EN

Stack Overflow用户

发布于 2013-01-31 01:45:53

代码语言:javascript
复制
$input = "http://steamcommunity.com/id/herpderp";
$output = '';

if( preg_match(':/:', $input) ) {
    $output = explode('/', $input);
    if( !empty($output[count($output)-1]) {
        $output = $output[count($output)-1];
    } else {
        // in the case of a trailing slash, use the ~second~ last field
        // ie: http://steamcommunity.com/id/herpderp/
        $output = $output[count($output)-2];
    }
} else {
    $output = $input;
}

echo "SteamID: " . $output;

或者简单地说:

代码语言:javascript
复制
if( preg_match('/[^a-zA-Z0-9]/', $input) ) {
    die('Disallowed characters in SteamID. Please enter only your SteamID, and not the full URL.');
}

我更喜欢第二种方法,因为它让用户处理自己的无能,而不是必须不断添加代码行来处理用户似乎设计的新的和创新的问题。他们总是努力证明,任何时候,只要有什么东西被宣布为“傻瓜证明”,宇宙只会产生一个更好的傻瓜。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14610067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档