php 一行行读取文本文件

 更新时间:2016年11月25日 15:59  点击:1520

例子

<?php

/**
 *
 * Read a line number from a file
 *
 * @param    string    $file    The path to the file
 * @param    int    $line_num    The line number to read
 * @param    string    $delimiter    The character that delimits lines
 * @return    string    The line that is read
 *
 */
function readLine($file, $line_num, $delimiter=" ")
{
    /*** set the counter to one ***/
    $i = 1;

    /*** open the file for reading ***/
    $fp = fopen( $file, 'r' );

    /*** loop over the file pointer ***/
    while ( !feof ( $fp) )
    {
        /*** read the line into a buffer ***/
        $buffer = stream_get_line( $fp, 1024, $delimiter );
        /*** if we are at the right line number ***/
        if( $i == $line_num )
        {
            /*** return the line that is currently in the buffer ***/
            return $buffer;
        }
        /*** increment the line counter ***/
        $i++;
        /*** clear the buffer ***/
        $buffer = '';
    }
    return false;
}
?>

Example Usage

<?php

/*** make sure the file exists ***/
$file = 'my_file.txt';

if( file_exists( $file ) && is_readable( $file ) )
{
    echo readLine($file, 6);
}
else
{
    echo "Cannot read from $file";
}
?>

This method of iterating over the file and seeking to the line number works well and is memory efficient, but it would be nice to have PHP do the work for us. PHP provides the SPL File Object which will do exactly what is required.


<?php

/*** the file to read ***/
$file = 'foo.txt';

       
/**
 *
 * Read a line number from a file
 *
 * @param    string    $file    The path to the file
 * @param    int    $line_num    The line number to read
 * @return    string    The line that is read
 *
 */
function readLine( $file, $line_number )
{
        /*** read the file into the iterator ***/
        $file_obj = new SplFileObject( $file );

        /*** seek to the line number ***/
        $file_obj->seek( $line_number );

        /*** return the current line ***/
        return $file_obj->current();
}

echo readLine( $file, 345678 );

?>

 

目录

   1.  什么是异常
   2. d 入门
   3. 自定义异常
   4.  多个异常
   5. 重新抛出异常
   6.  程序流的异常
   7.  顶层异常处理程序
   8. 错误代码
   9. 摘要

 什么是例外。

.随着PHP 5的到来,有了新的面向对象模型和一个新的对象的处理错误导向的方法。 .例外给我们很大的一个更好的错误让我们自定义的脚本错误时的行为(异常)遇到的处理。 此前PHP的处理功能,通过让来自错误标志和trigger_error()函数错误。 这些都是良好,良好的前的php5天,但在新的面向对象的环境中,我们需要更大的控制权。

我们有好的干净的代码处理教程。 在本教程中,我们将处理问题,以及如何处理这些问题。
Getting started. 入门。

.最简单的形式,例外情况仍然非常强大,因为它们使我们能够发现错误的地点和时间,我们想要的。.在一个更复杂的形式异常给我们一个替代的特殊的错误返回代码使用的信号函数调用失败。 我们可以使用各种功能的代码整个阻止他们。可以让潜水的,看看什么大惊小怪。 可让创建一个名为exception.php ...

<?php

/**
*
* @Check if number is greater than 3
*
* @param int
*
* @return bool on success
*
*/
function checkNum ( $number ){
if( $number > 3 )
{
throw new Exception ( "Number is greater than 3" );
}
return true ;
}

/*** call the function ***/
checkNum ( 28 );

?>

上面的代码将产生这样的错误:
Fatal error: Uncaught exception 'Exception' with message 'Number is greater than 3' in /www/exception.php:15 Stack trace: #0 /www/exception.php(21): checkNum(28) #1 {main} thrown in /www/exception.php on line 15致命错误:未捕获的异常异常与信息'数'大于3'/万维网/ exception.php:15堆栈跟踪:#0 /万维网/ exception.php(21):checkNum(28)#1(主要)抛出/万维网/在线15 exception.php

.该错误是因为发生了异常已在checkNum函数中抛出的,但尚未落网。.所有的异常必须被捕获或将出现此错误。.一个例外是被发现使用一个 catch块。但首先我们需要尝试的代码引发异常。 So the order of things is:因此,事物的顺序是:

    * try 尝试
    * throw 投
    * catch 捕捉

.记住,如果你扔东西,你必须抓住它。  code.能让把它放到了一些有效的代码测试。

<?php
/**
*
* @Check if number is greater than 3
*
* @param int
*
* @return bool on success
*
*/
function checkNum ( $number ){
if( $number > 3 )
{
throw new Exception ( "Number is greater than 3" );
}
return true ;
}

/*** try block ***/
try
{
checkNum ( 28 );
/*** this code does not get excecuted if an exception is thrown ***/
echo 'If you see this, the number is not greater than 3' ;
}
/*** catch the exception here ***/
catch( Exception $e )
{
// code to handle the Exception
echo 'Catch exception here<br />' ;
echo 'Message: ' . $e -> getMessage (). '<br />' ;
echo 'More text or functions here.' ;
}
?>

当访问,上面的代码将产生输出到浏览器与此类似:
Catch exception here这里捕获异常
3消息:数大于3
.更多的文本或职能这里。

可以让单步执行代码,并获得一个正在发生的事情的抓地力。.函数checkNum简单地检查是一个数字大于3。 如果不是,异常与抛出的错误消息。 其次,我们有一个尝试()块作出调用checkNum()函数。 如果在checkNum条件()函数并不满足,exeption抛出,将在第一个catch块中捕获。.您可能有多个catch块。 PHP将试图解析出现任何异常,直至在这个代码块抛出或代码块结束。 Any code after an exception is thrown is not executed.以后的任何异常的代码不会被执行。

In the catch(){ } 代码的第一呼应一行文字说我们已经赶上了异常,则错误信息包含在调用$电子“getMessage()方法的异常类。 .错误信息是直接传递给构造异常类。 当捕捉抛出的异常,我们分配给一个变量的实例(对象)或代码中使用,而现在我们有一个异常对象,其中包含我们的异常信息。 So, enough of the basics.因此,足够的基础。 我们按照这份文件,我们将探讨更深...
Custom Exceptions 自定义异常

.异常类最喜欢的课程,可以扩展到创建自己的自定义异常类。 当一个新的类是基于PHP使用母公司统一施工,并允许的传承classAs。 .因此,当异常类扩展,所有方法都可以使用。 能让采取一类使用一个无效的电子邮件地址检查的基础上filter_var()函数返回的值运行它。

<?php
class customException extends Exception {

/**
*
* @return exception message
*
* @access public
*
* @return string
*
*/
public function exceptionMessage (){
/*** the error message ***/
$errorMsg = 'The email Address ' . $this -> getMessage (). ', on line ' .
$this -> getLine (). ' in ' . $this -> getFile (). ', is invalid.' ;
/*** return the error message ***/
return $errorMsg ;
}

} /*** end of class ***/
?>

The class above is essentially an exception class on its own.上面的类本质上是对自己的异常类。 It has the single addition of the exceptionMessage() method that calls the Exception class methods getLine() and getFile() and getMessage().它具有单一的exceptionMessage除了()方法调用的异常处理类方法getLine()和的GetFile()和getMessage()。 As they are defined in the base class, the extended class *inherits* from it.由于他们是在基类中定义的,扩展的类* *从它继承。 Lets take it for a test drive.能让采取试驾它。

<?php

/*** an invalid email address ***/
$email = "example@phpro@org" ;
try    {
/*** check the validity of the email address ***/
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
/*** if there is a problem, throw an exception ***/
throw new customException ( $email );
}
}
catch ( customException $e )
{
/*** display the custom error message ***/
echo "<p style='color: red'>" . $e -> errorMessage (). "</p>" ;
}

/*** the custome excetpion class ***/
class customException extends Exception {
/**
*
* @return exception message
*
* @access public
*
* @return string
*
*/
public function errorMessage (){
/*** the error message ***/
$errorMsg = 'The email Address ' . $this -> getMessage (). ', on line ' .
$this -> getLine (). ' in ' . $this -> getFile (). ', is invalid.' ;
/*** return the error message ***/
return $errorMsg ;
}

} /*** end of class ***/

?>

显然,提供的电子邮件地址是无效的,当filter_var()函数返回布尔假抛出一个异常的customException类。 .抛出异常的将任何正常例外。 即使只参数传递给customException类是变量$电子邮件,所有可用的方法父类可异常,如getMessage(),getLine()和的GetFile()。.由于PHP的面向对象模型包括统一的构造,当变量$电子邮件传递给customException类,它可以立刻提供给父类。.但这个例子表明,只有如何尝试,投掷,并抓到一个例外。.还有的时候,我们需要比这更多。 多个异常可以抛出并捕获。
Multiple Exceptions 多个异常

多个异常可以采取多种形式。 .他们可以在多的if / else块,在一个交换机或嵌套。 Yes, you can nest exceptions in multiple try{ } catch(){ } blocks.是的,你可以尝试在多个巢捕获异常()()()块。 A script may need to check for multiple conditions and different messages returned.阿脚本可能需要检查多个条件和不同的消息返回。 Program flow may also need to change if an exception is thrown.程序流程可能也需要改变,如果抛出一个异常。 Lets see how we can nest an exception and check for different error conditions.让我们看看我们如何可以嵌套异常和不同的错误条件检查。 The customException class need not change for this.在customException类并不需要改变这一点。 This is what is meant in PHP as re-usable code.这就是在PHP中的意思是可重复使用的代码。 Here we see an example of using nested try{ } catch(){ } blocks.这里我们看到一个使用嵌套的尝试,例如()赶上()()块。

<?php
error_reporting ( E_ALL );

$email = "example@phpro.org" ;
try    {
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
throw new customException ( $email );
}
try    {
if( strpos ( $email , 'phpro' ) !== FALSE )
{
throw new Exception ( " $email is funky" );
}
}
catch( Exception $e )
{
echo '<p style="color: blue">' . $e -> getMessage (). '</p>' ;
}
}
catch ( customException $e )
{
echo "<p style='color: red'>" . $e -> errorMessage (). "</p>" ;
}

class customException extends Exception {

/**
*
* @return exception message
*
* @access public
*
* @return string
*
*/
public function errorMessage (){
/*** the error message ***/
$errorMsg = 'The email Address ' . $this -> getMessage (). ', on line ' .
$this -> getLine (). ' in ' . $this -> getFile (). ', is invalid.' ;
/*** return the error message ***/
return $errorMsg ;
}

} /*** end of class ***/

?>

In the above example, the colors of the errors have been made red or blue to easily discern which error has been thrown.在上面的例子中,这些错误的颜色作出了红色或蓝色,不难看出其中的错误已被抛出。 The problem with this sort of code, or nested if/else blocks, is if the program calls for large amounts of validation, we end up with very deeply nested code which can become very clumsy to read and debug.与此代码排序,嵌套或问题的if / else块,如果程序是需要大量的验证,我们最终非常深嵌套代码,可能会变得非常笨拙的阅读和调试。 A better option is simply to code smarter.Lets look at an option using only single if() blocks to check a condition.一个更好的选择就是代码smarter.Lets在一个单一的,如果只使用()块检查条件的选项。

<?php
error_reporting ( E_ALL );

$email = "kevin@fish.phpro.org" ;
try    {
/*** check if email is valid ***/
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
throw new customException ( $email );
}
/*** check for fish ***/
if( strpos ( $email , 'fish' ) !== FALSE )
{
throw new Exception ( " $email is funky" );
}
/*** check for example.org ***/
if( $email !== 'kevin@example.org' );
{
throw new customException ( $email );
}
}
catch( customException $e )
{
echo '<p style="color: red">' . $e -> errorMessage (). '</p>' ;
}
catch( Exception $e )
{
echo '<p style="color: blue">' . $e -> getMessage (). '</p>' ;
}

class customException extends Exception {

/**
*
* @return exception message
*
* @access public
*
* @return string
*
*/
public function errorMessage (){
/*** the error message ***/
$errorMsg = 'The email Address ' . $this -> getMessage (). ', on line ' .
$this -> getLine (). ' in ' . $this -> getFile (). ', is invalid.' ;
/*** return the error message ***/
return $errorMsg ;
}

} /*** end of class ***/
?>

.上面的编码风格使得好多意识和更容易跟踪和阅读。 .两个catch()()块中采用了此代码的执行情况作为例外,允许倍数。 再次是错误的颜色编码,这一次,我们看到,PHP已经成功跃升至正确的捕获()()块对应抛出错误。 在这种情况下,美元的电子邮件变量的*鱼*,因此在它的标准异常,并在适当的catch捕获()()块字。 块被省略,只有基地异常catch块是用,将处理异常那里。
Re-throwing Exceptions 重新抛出异常

一个例外可以重新抛出当你希望执政程序流.如果条件不满足,并抛出一个异常,你可以有不同的处理的基础上第二个条件,或者您不妨尝试从错误中恢复。 人们普遍认为,从实践中隐藏用户的系统错误。让我们考虑与专业发展主任简单的数据库连接。 因为PDO是完全如此惊人的,它有一个内置的例外。 .如果发生错误,当试图连接到一个异常的,必须抓住数据库。虽然在从数据库错误可能是有意义的人编码的应用程序,它有很少或没有相关的用户。.因此,异常可以重新用了漂亮友好的信息,即用户更容易理解抛出。例如:一个失败的专业发展主任数据库错误是这样的:
无法打开数据库文件
当重新掷下级这样的错误,例外必须用内被抛出一个。 如果异常没有抓住包装,将继续“泡沫了”堆栈,直到找到一个catch()()块来处理它。如果没有赶上()()块找到,未捕获的异常错误的结果。下面的例子演示了如何正确地重新抛出一个异常。

<?php
try    {
try    {
/*** try to connect to a non-existant database ***/
$db = new PDO ( "sqlite:/path/to/database.sdb" );
}
catch( PDOException $e )
{
/*** rethrow the error to the custom handler ***/
throw new customException ( "Database unavailable" );
}
}
/*** catch the customException here ***/
catch( customException $e )
{
/*** echo the new error message ***/
echo $e -> errorMessage ();
}

class customException extends Exception {

/**
*
* @return exception message
*
* @access public
*
* @return string
*
*/
public function errorMessage (){
/*** the error message ***/
$errorMsg = '<p style="color: red;">' . $this -> getMessage (). '</p>' ;
/*** return the error message ***/
return $errorMsg ;
}

} /*** end of class ***/
?>

.从上面的代码中,我们看到一个PDO异常当我们试图连接到一个不存在的数据库。 .这一例外是捕获并重新与customException类处理错误,并优雅地退出抛出。 Any un-.任何未捕获的异常应被视为一个应用程序错误。
Program Flow with Exceptions 程序流的异常

例外不应被用作藤,即使他们似乎理想目的的理想选择 try{} catch{} blocks.一个好的例外implemtation意味着代码将未经尝试()()块捕获任何。.异常类应该只用于错误条件。 下面是一个不正确的异常使用的例子。

<?php

$animalsArray = array( 'dingo' , 'wombat' , 'steve irwin' , 'kiwi' , 'kangaroo' , 'platypus' );
try    {
/*** loop over the array ***/
foreach( $animalsArray as $value )
{
/*** if we find a kiwi ***/
if( $value = 'kiwi' )
{
/*** throw an exception here ***/
throw new Exception ( "This is not an Australian Native" );
}
}
}
catch( Exception $e )
{
/*** catch the exception here ***/
echo $e -> getMessage ();
}
?>

.上面的脚本抛出一个值时,在foreach循环发现异常。 这是一个例外类作为一个简单的突破这里滥用后会做同样的工作干净。 没有错误发生,所以应该没有异常抛出。
Top Level Exception Handler 顶层异常处理程序

.阿PHP最惊人的特点是set_exception_handler()函数。 .这个小魔术师,照顾的联合国陷入数额是多少例外最后机会赶上任何先前未捕获的异常,即:如果没有任何异常在catch()()块捕获。.此函数作为其唯一的参数回调exception_handler前必须给它的任何要求定义。 该default_handler将一个参数是异常对象已抛出。 .这列出了脚本的运作。


<?php

/*** a default Exception handler ***/
function my_default_handler ( $exception ){
echo "Uncaught exception: " , $exception -> getMessage ();
}

/*** set the default to handler to my_default_handler ***/
set_exception_handler ( 'my_default_handler' );

/*** throw an exception ***/
throw new Exception ( 'Catch me if you can!' );

echo 'This is not executed' ;

?>

If you wanted to restore the default Exception handler, simple use the restore_exception_handler(); function.如果你想恢复默认异常处理程序,简单使用restore_exception_handler()函数。 This could also be used if your current default handler was another custom Exception class.这也可以使用如果您当前的默认处理程序是另一个自定义异常类。 The Exception classes are held in a stack can be restored with the restore_exception_handler() function as shown here.异常类被关押在一个栈可以与restore_exception_handler恢复()函数,如下所示。

<?php

/*** an email to validate ***/
$email = 'guru@phpro.org' ;

/*** red error messages ***/
function blue_default_handler ( $exception ){
echo '<p style="color: blue;">' . $exception -> getMessage (). '</p>' ;
}

/*** blue error messages ***/
function red_default_handler ( $exception ){
echo '<p style="color: red;">' . $exception -> getMessage (). '</p>' ;
}

/*** set the default to handler to red_default_handler ***/
set_exception_handler ( 'red_default_handler' );

/*** some code here ***/
if(!isset( $email ))
{
/*** if there is no match ***/
throw new exception ( 'No Email is set' );
}

/*** set the default handler to blue_default_handler ***/
set_exception_handler ( 'blue_default_handler' );

/*** check the email value ***/
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
throw new Exception ( 'Invalid Email' );
}

/*** restore the default handler ***/
restore_exception_handler ();

if( strpos ( $email , 'guru' ) !== false )
{
throw new Exception ( 'Sorry, No gurus at phPro' );
}

echo 'This is not executed' ;

?>

So, from the script above we see the declaration of two Exception handlers, one makes the error messags red, and the other makes them blue.因此,从上面我们看到的是两个异常处理声明脚本,其中一个人的错误messags红色,蓝色等使它们。 This is perhaps an abuse of the Exception functionality but serves well to demonstrate changes in classes as they occur.这也许是一个异常功能的滥用,但可以很好地显示在课堂发生变化。 The initial default handler is set to red.最初的默认处理程序设置为红色。 The a check is run to see if an $email variable is set.在运行检查,看看是否$电子邮件变量。 Try removing the $email variable or commenting it out to see it work.尝试删除电子邮件美元的变量或评论出来才能看到它的工作。 If the $email varible is set, the code continues and the default handler is set to the blue_default_handler.如果$电子邮件varible设置,代码继续进行,默认处理程序设置为blue_default_handler。 Then using the filter_var() function, the $email is tested for validity and if it fails, a blue message is shown.然后使用filter_var()函数,电子邮件美元的有效性进行测试,如果失败,一个蓝色的消息显示。 Try changing the value of the email to 1234 to see the results.试着改变电子邮件的价值1234看到的结果。 Finally we see the use of restore_exception_handler(); which pops the stack and returns the default Exception handler to the red_default_handler.最后,我们看到了restore_exception_handler使用();的弹出堆栈,并返回默认的异常处理程序red_default_handler。 When any of these exceptions occur, code execution stops and control is passed to the handler.当这些异常的发生,代码执行停止和控制传递给处理程序。 You notice at the bottom of the script above that the line您会注意到在上述脚本的底部的行
echo 'This is not executed';回声'这是不执行';
is never executed.永远不会执行。 But..但.. what if we wish to continue with the script and execute code below the thrown exceptions?如果我们希望继续与脚本和执行下面的代码抛出的例外? This is the default behavior of Exceptions.这是例外的默认行为。 Once outside the catch(){ } block you are free to include whatever code you wish.一旦捕捉外()()块你可以自由地包括任何代码的愿望。

Output buffering is Off by default in php.ini so it will need to be turned on via ob_start().输出缓冲是在php.ini文件的默认关闭,因此它需要被关闭的通过ob_start()。 We can begin output to the brower in the script and if an exception is thrown, still use header() to redirect based on the exception type.我们可以开始输出到在脚本布劳尔,如果抛出异常,仍使用头()重定向的异常类型为基础。

<?php
try {
/*** try to connect to a non-existent database ***/
$dsn = new PDO ( "sqlite:/path/to/database.php" );

/*** this code is never executed ***/
echo 'Move along, nothing to see here' ;

}
catch ( PDOException $e )
{
/*** echo the error message ***/
echo '<h4>Database Error: ' , $e -> getMessage (), '</h4>' ;
}

/*** more code and HTML here ***/
echo '<p>Catch me if you can!<p>' ;

?>
Error Codes 错误代码

As seen in the beginning of this document, the Exception class contains a getCode() method that will fetch an error code, this can useful as you can define your own error codes as well as your own error messages.正如本文开头所看到的,异常类包含一个认证码()方法,将获取错误代码,这可以帮助你可以定义自己的错误代码,以及自己的错误信息。 This quick demonstration shows how.这显示了如何快速演示。

<?php

/*** an invalid email address ***/
$email = 'example@phpro@org' ;

try    {
/*** validate the email address ***/
$db = validateEmail ( $email );
}
catch ( Exception $e )
{
/*** display the error code ***/
echo 'Error: ' . $e -> getCode ();
}

/**
*
* @validate an email
*
* @param string
*
* @throw standard exception on failure
*
*/
function validateEmail ( $email ){
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
throw new Exception ( "Invalid Email" , 10 );
}
}

?>

The above code returns the line:上面的代码返回行:
Error: 10错误:10
Well, big deal you say..好吧,大不了你说.. Ok, if we can define our own error codes, we can script based on thier value!好吧,如果我们可以定义自己的错误代码,脚本,我们可以根据他们的价值! The following script shows some of the possibilities.下面的脚本显示了一些可能性。

<?php

/*** an invalid email address ***/
$email = 'example@phpro@org' ;

try    {
/*** validate the email address ***/
$db = validateEmail ( $email );
}
catch ( Exception $e )
{
/*** display the error code ***/
switch ( $e -> getCode ()) {
case 0 :
echo 'Handle error here' ;
break;
case 10 :
echo 'Handle error condition here' ;
break;
case 20 :
echo "handle other condition here" ;
break;
default:
echo 'some default handler here' ;
}
}

/**
*
* @validate an email
*
* @param string
*
* @throw standard exception on failure
*
*/
function validateEmail ( $email ){
if( filter_var ( $email , FILTER_VALIDATE_EMAIL ) === FALSE )
{
throw new Exception ( "Invalid Email" , 10 );
}
}

?>

<?php

try
{
    /*** Read in the animated gif ***/
    $animation = new Imagick("animation.gif");

    /*** Loop through the frames ***/
    foreach ($animation as $frame)
    {
        /*** Thumbnail each frame ***/
        $frame->thumbnailImage(100, 100);
   
        /*** Set virtual canvas size to 100x100 ***/
        $frame->setImagePage(100, 100, 0, 0);
    }

    /*** Write image to disk. Notice writeImages instead of writeImage ***/
    $animation->writeImages("animation_thumbnail.gif");

    echo "Images written";
}
catch(Exception $e)
{
    echo $e->getMessage();
}

?>

 

<?php

    error_reporting(E_ALL);

    $width = 80;

    /*** the image file to thumbnail ***/
    $image = 'spork.jpg';

    if(!file_exists($image))
    {
        echo 'No file found';
    }
    else
    {
        /*** image info ***/
        list($width_orig, $height_orig, $image_type) = getimagesize($image);

        /*** check for a supported image type ***/
        if($image_type !== 2)
        {
            echo 'invalid image';
        }
        else
        {
            /*** thumb image name ***/
            $thumb = 'thumb.jpg';

            /*** maintain aspect ratio ***/
            $height = (int) (($width / $width_orig) * $height_orig);

            /*** resample the image ***/
            $image_p = imagecreatetruecolor($width, $height);
            $image = imageCreateFromJpeg($image);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

            /*** write the file to disc ***/
            if(!is_writeable(dirname($thumb)))
            {
                echo 'unable to write image in ' . dirname($thumb);
            }
            else
            {
                imageJpeg($image_p, $thumb, 100);
            }
        }
    }
?>

 

 <?php
    /**
    *
    * Validate a date
    *
    * @param    string    $date
    * @param    string    format
    * @return    bool
    *
    */
    function validateDate( $date, $format='YYYY-MM-DD')
    {
        switch( $format )
        {
            case 'YYYY/MM/DD':
            case 'YYYY-MM-DD':
            list( $y, $m, $d ) = preg_split( '/[-./ ]/', $date );
            break;

            case 'YYYY/DD/MM':
            case 'YYYY-DD-MM':
            list( $y, $d, $m ) = preg_split( '/[-./ ]/', $date );
            break;

            case 'DD-MM-YYYY':
            case 'DD/MM/YYYY':
            list( $d, $m, $y ) = preg_split( '/[-./ ]/', $date );
            break;

            case 'MM-DD-YYYY':
            case 'MM/DD/YYYY':
            list( $m, $d, $y ) = preg_split( '/[-./ ]/', $date );
            break;

            case 'YYYYMMDD':
            $y = substr( $date, 0, 4 );
            $m = substr( $date, 4, 2 );
            $d = substr( $date, 6, 2 );
            break;

            case 'YYYYDDMM':
            $y = substr( $date, 0, 4 );
            $d = substr( $date, 4, 2 );
            $m = substr( $date, 6, 2 );
            break;

            default:
            throw new Exception( "Invalid Date Format" );
        }
        return checkdate( $m, $d, $y );
    }
?>

Example Usage

<?php
        echo validateDate( '2007-04-21' ) ? 'good'. " " : 'bad' . " ";
        echo validateDate( '2007-21-04', 'YYYY-DD-MM' )  ? 'good'. " " : 'bad' . " ";
        echo validateDate( '2007-21-04', 'YYYY/DD/MM' )  ? 'good'. " " : 'bad' . " ";
        echo validateDate( '21/4/2007', 'DD/MM/YYYY' )  ? 'good'. " " : 'bad' . " ";
        echo validateDate( '4/21/2007', 'MM/DD/YYYY' )  ? 'good'. " " : 'bad' . " ";
        echo validateDate( '20070421', 'YYYYMMDD' )  ? 'good'. " " : 'bad' . " ";
        echo validateDate( '04212007', 'YYYYDDMM' )  ? 'good'. " " : 'bad' . " ";
?>

 

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • php把读取xml 文档并转换成json数据代码

    在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29
  • C#使用StreamWriter写入文件的方法

    这篇文章主要介绍了C#使用StreamWriter写入文件的方法,涉及C#中StreamWriter类操作文件的相关技巧,需要的朋友可以参考下...2020-06-25
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
  • C#路径,文件,目录及IO常见操作汇总

    这篇文章主要介绍了C#路径,文件,目录及IO常见操作,较为详细的分析并汇总了C#关于路径,文件,目录及IO常见操作,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25