当人们在我的网站上编辑他们的帐户时,他们感到困惑,认为他们必须键入所有的蒸汽配置文件链接,例如http://steamcommunity.com/id/######,而他们只需在文本字段中键入#######,那么如果他们搞砸了,我如何从他们的输入中删除除#####部件之外的所有内容?
发布于 2013-01-31 01:30:41
这样如何:
$lastPart = array_pop(explode('/', $_SERVER['REQUEST_URI']));这将检索REQUEST_URI的最后一个元素。
发布于 2013-01-31 01:31:23
str_replace("http://steamcommunity.com/id/","",$input);是一种方法。
一种更健壮的方式是:
preg_replace("@^.*id/@","",$input);
发布于 2013-01-31 01:45:53
$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;或者简单地说:
if( preg_match('/[^a-zA-Z0-9]/', $input) ) {
die('Disallowed characters in SteamID. Please enter only your SteamID, and not the full URL.');
}我更喜欢第二种方法,因为它让用户处理自己的无能,而不是必须不断添加代码行来处理用户似乎设计的新的和创新的问题。他们总是努力证明,任何时候,只要有什么东西被宣布为“傻瓜证明”,宇宙只会产生一个更好的傻瓜。
https://stackoverflow.com/questions/14610067
复制相似问题