我需要验证albumID输入是2个大写字母和3个数字。我使用http://regexr.com/和http://www.tutorialspoint.com/php/php_preg_match.htm来确认此
<?php
// define variables and set to empty values
$albumidErr = $albumid = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (preg_match('/([A-Z]{2}[0-9]{3})/', $albumid))
{
$albumid = test_input($_POST["albumid"]);
$albumidErr = print $albumid;
}
else {
$albumidErr = " Album ID must be 2 Uppercase letters and 3 numbers (i.e. BI010)";
}
}
?>
<form method="post" action="
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>
<label for="albumid">*Album ID:</label>
<?php echo $albumidErr;?><br>
<input type="text" name="albumid" id="albumid"/>
</p>
<p>
<input type="submit" name="submit" id="submit" value="submit"/>
</p>
</form>
应该有用,但不是:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIScrollViewDelegate {
var imageView = UIImageView()
@IBOutlet var camera: UIButton!
@IBOutlet weak var photoLibrary: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet var imageDisplay: UIImageView!
@IBAction func photoLibraryAction(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
@IBAction func cameraAction(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage; dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)
imageView.image = UIImage(named: "image")
imageView.userInteractionEnabled = true
scrollView.addSubview(imageView)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.loadImage(_:)))
tapGestureRecognizer.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tapGestureRecognizer)
}
func loadImage (recognizer:UITapGestureRecognizer){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
imageView.contentMode = UIViewContentMode.Center
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height)
scrollView.contentSize = image.size
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleHeight, scaleWidth)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1
scrollView.zoomScale = minScale
centerScrollViewContents()
picker.dismissViewControllerAnimated(true, completion: nil)
}
func centerScrollViewContents(){
let boundsSize = scrollView.bounds.size
var contentsFrame = imageView.frame
if contentsFrame.size.width < boundsSize.width{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2
}else{
contentsFrame.origin.x = 0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2
}else{
contentsFrame.origin.y = 0
}
imageView.frame = contentsFrame
}
func scrollViewDidZoom(scrollView: UIScrollView) {
centerScrollViewContents()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
// Do any additional setup after loading the view, typically from a nib.
}
func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:0)
在您的代码中:
$albumidErr = $albumid = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (preg_match('/([A-Z]{2}[0-9]{3})/', $albumid))
您正在使$albumid
为空,然后您尝试将正则表达式与此空字符串匹配,当然,它并不匹配。
我想你想要:
$albumidErr = $albumid = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (preg_match('/([A-Z]{2}[0-9]{3})/', $_POST["albumid"]))
// ^^^^^^^^^^^^^^^^^
你可以添加锚点:
$albumidErr = $albumid = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (preg_match('/^([A-Z]{2}[0-9]{3})$/', $_POST["albumid"]))