简体   繁体   中英

Read File Content Php

I am trying to read the upper most comments of a style sheet using php, what i want is on/ce the comments get over php should stop reading that file let me show you my code

$handle = fopen($filename,"r");
        if ($handle)
        {
            while (($file = fgets($handle, 4096)) !== false)
            {
                if($file != '*/')
                {
                echo $buffer.'<br />';
                }
                else
                {
                    break;
                }
            }
        }

What i am trying to do is reading the file line by line and the movement my line is equal to the sign of ending css comments it should not run that while statement but that is not working right now

how about adding trim, to deal with whitespace & the line ending "\\r\\n"s

$handle = fopen($filename,"r");
        if ($handle)
        {
            while (($file = fgets($handle, 4096)) !== false)
            {
                if (trim($file) == '*/') {
                    break;
                }
                echo $buffer.'<br />';
            }
        }

据我所知,您的缓冲区变量从未设置,请重新看一下http://www.php.net/manual/en/function.fgets.php

$handle = fopen($filename,"r");
        if ($handle)
        {
            while (($file = fgets($handle, 4096)) !== false)
            {
                if($file != '*/')
                {
                echo $buffer.'<br />';
                }
                else
                {
                    break;
                }
            }
        }

$buffer never gets a value. Here's how it should look like:

$handle = fopen($filename,"r");
        if ($handle)
        {
            while (($buffer = fgets($handle, 4096)) !== false)
            {
                if($buffer != '*/')
                {
                echo $buffer.'<br />';
                }
                else
                {
                    break;
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM