我正在使用刷卡来获取id,并使用它将考勤存储到数据库中。我有一个带有jQuery的表单,当用户在输入字段中输入8个字符串时,它会自动提交表单。我想用刷卡的数据来做这件事,但当我刷卡时,我得到的输出是;910522804?
,它应该是k1052280
。
我怎么能修剪数据,这样当有人刷卡时,就可以去掉; ? and the last digit
了。如有任何帮助,我们将不胜感激。
Form和jQuery代码就是这个
<form id="SwipeForm" action="Swiped.php" method="post">
<input id="Swipe" name="studentID" type="text" value="" />
<input type="submit" value="Submit" />
</form>
<script>
$(function () {
$('#Swipe').bind('DOMAttrModified textInput input change keyup keypress paste', function() {
var vallength = $(this).val().length;
if (vallength == 8) {
$('#SwipeForm').submit();
}
});
});
</script>
<form id="SwipeForm" action="Swiped.php" method="post">
<input id="Swipe" name="studentID" type="text" value="" />
<input type="submit" value="Submit" />
</form>
您可以使用strlen函数和substr来获取字符串的一部分。
$str = ';910522804?';
$str = substr($str, 1, strlen($str) - 2);
var_dump($str);
要删除第一个字符和最后两个字符,嵌套的substr
函数起作用:
$str = ';910522804?';
$str = substr(substr($str, 1), 0, -2);
echo $str;
// 91052280