如何在php中添加新行

时间:2015-10-08 15:00:56

标签: php validation

我想在每次验证测试后给出空格和新行:

<?php

class User{

    private $first_name;
    private $last_name;
    private $email;

public function __construct() {

    }

      public function __set($name, $value) {
        $method = "set_$name";
        if(!method_exists($this, $method)){
            throw new Exception("SET Property $name does not exist");
        }
        $this->$method($value);

    }

    public function __get($name) {
        $method = "get_$name";
        if(!method_exists($this, $method)){
            throw new Exception("GET Property $name does not exist");
        }
        return $this->$method();
    }
    //First name     
        private function set_first_name($first_name){
            $reg = "/^[a-z]+$/i";

                if(!preg_match($reg, $first_name)){
                    throw new Exception("Invalid/Missing First Name");
                }

            $this->first_name = $first_name;
        }

        private function get_first_name(){
            return $this->first_name;
    }
    //Last Name
        private function set_last_name($last_name){
        $reg = "/^[a-z]+$/i";

        if(!preg_match($reg, $last_name)){
                    throw new Exception ("Invalid/Missing Last Name");

                }
               $this->last_name =$last_name;
        }      
        private function get_last_name(){
           return $this->last_name;

        }
    //Email
        private function set_email($email){
        //  $reg = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
        $reg = "/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/";  
          if(!preg_match($reg, $email)) {
              throw new Exception("Invalid/Missing Email");
                }
          $this->email = $email;
        }
         private function get_email(){
         return $this->email;

        }

    //username
           private function set_username($username){
              $reg = "/^[a-z\d_]{5,20}$/i";

               if(!preg_match ($reg,$username)) {
                   throw new Exception ("Invalid/Missing Username");
               }
               $this->username = $username;

               }
            private function get_username() {
                return $this->username;
           }
}

//constructor called
$obj_user = new User();

//First Name
try {
    $obj_user->first_name = "Ali";
    echo("$obj_user->first_name");

} catch (Exception $ex) {
    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");


}

//last Name

try {
    $obj_user->last_name = "Asif";
    echo("$obj_user->last_name");


} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}

//email
try {
    $obj_user->email = "ali@yahoo.com";
    echo("$obj_user->email");


} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}

//username

try {
    $obj_user->username = "aliqayyum";
    echo("$obj_user->username");


} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}

输出是这样的:

AliAsifali@yahoo.comaliqayyum(322) 802-729811/10/1986

我想这样显示:

Ali Asif 
ali@yahoo.com
aliqayyum
(322) 802-7298
11/10/1986

3 个答案:

答案 0 :(得分:0)

Juste在你的捕获异常中添加“br”

//First Name
try {
    $obj_user->first_name = "Ali";
    echo("$obj_user->first_name");
    echo("<br />"); //br is an HTML tag for new line

} catch (Exception $ex) {
    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");
}

答案 1 :(得分:0)

将换行添加到每个输出字符串。所以例如

echo("$obj_user->email");

会变成

echo("$obj_user->email"."\n");

答案 2 :(得分:0)

<?php

class User{

    private $first_name;
    private $last_name;
    private $email;

public function __construct() {

    }

      public function __set($name, $value) {
        $method = "set_$name";
        if(!method_exists($this, $method)){
            throw new Exception("SET Property $name does not exist");
        }
        $this->$method($value);

    }

    public function __get($name) {
        $method = "get_$name";
        if(!method_exists($this, $method)){
            throw new Exception("GET Property $name does not exist");
        }
        return $this->$method();
    }
    //First name     
        private function set_first_name($first_name){
            $reg = "/^[a-z]+$/i";

                if(!preg_match($reg, $first_name)){
                    throw new Exception("Invalid/Missing First Name");
                }

            $this->first_name = $first_name;
        }

        private function get_first_name(){
            return $this->first_name;
    }
    //Last Name
        private function set_last_name($last_name){
        $reg = "/^[a-z]+$/i";

        if(!preg_match($reg, $last_name)){
                    throw new Exception ("Invalid/Missing Last Name");

                }
               $this->last_name =$last_name;
        }      
        private function get_last_name(){
           return $this->last_name;

        }
    //Email
        private function set_email($email){
        //  $reg = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
        $reg = "/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/";  
          if(!preg_match($reg, $email)) {
              throw new Exception("Invalid/Missing Email");
                }
          $this->email = $email;
        }
         private function get_email(){
         return $this->email;

        }

    //username
           private function set_username($username){
              $reg = "/^[a-z\d_]{5,20}$/i";

               if(!preg_match ($reg,$username)) {
                   throw new Exception ("Invalid/Missing Username");
               }
               $this->username = $username;

               }
            private function get_username() {
                return $this->username;
           }
}

//constructor called
$obj_user = new User();

//First Name
try {
    $obj_user->first_name = "Ali";
    echo("$obj_user->first_name");
    echo("<br />");
} catch (Exception $ex) {
    echo($ex->getMessage()."<br");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");


}

//last Name

try {
    $obj_user->last_name = "Asif";
    echo("$obj_user->last_name");


} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}

//email
try {
    $obj_user->email = "ali@yahoo.com";
    echo("$obj_user->email");
    echo "<br>";

} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}

//username

try {
    $obj_user->username = "aliqayyum";
    echo("$obj_user->username");
    echo "<br>";


} catch (Exception $ex) {

    echo($ex->getMessage() . "<br>");
    echo($ex->getFile() . "<br>");
    echo($ex->getLine() . "<br>");

}