I need to remove empty lines when reading a file, and skip unnecessary lines to pass only the URL of the address to the parse_url function

File contents:

http: // shurik: qwerty@my.site.com: 9191 / documents.php? documentId = 128 & type = pdf # content

http: // shurik: qwerty@my.site.com: 9191 / 211.php? photoId = 128


http://www.google.com.ua:8080/catalog/folder/myname.php?answer=123&url=www.iai.ua

My code is:

<html> <head> </head> <body> <form method="get" action="1.php"> <br /> <p>Путь к файлу: <input name="file" type="text" size="50" tabindex="1" > &nbsp;&nbsp;&nbsp;<input type="submit" value="Ок" tabindex="2"></p> </form> <?php if (isset($_GET['file'])/*$_GET['submit']="Ok"*/) { echo "<div align=\"center\">\n<h2>Результаты:</h2>\n<div align=\"left\" style=\"width:75%\">\n"; $file = stripslashes(htmlspecialchars(escapeshellcmd(trim($_GET['file'])))); echo "Открытие файла: \"{$file}\"..."; if (file_exists($file)) { echo " файл найден!\n"; echo "<h3>Содержимое файла:</h3>"; $fp = file($file); $flines = count($fp); $arr = array(); for ($i = 0; $i < $flines; $i++) { echo $fp[$i]."<br>"; $url = $fp[$i]; $t = array ( 'scheme' => 'Протокол', 'host' => 'Хост', 'port' => 'Порт', 'user' => 'Юзер', 'pass' => 'Пароль', 'path' => 'Путь к файлу', 'query' => 'Параметры запроса', 'fragment' => 'Фрагмент' ); $arr = parse_url($url); foreach ($arr as $key => $val) { echo $t[$key].': '. $val."<br>"; } } } else { echo " файл не найден!\n"; } echo "</div>\n</div>\n"; } ?> </body> </html> 

But it turns out only this way: alt text

And the __ sign in fragment is superfluous, where does it come from?

    1 answer 1

    1. You can try to assume that each url should begin with http

    so

     for ($i = 0; $i < $flines; $i++) { echo $fp[$i]."<br>"; 

    Replace with:

     for ($i = 0; $i < $flines; $i++) { if (strpos(strtolower($fp[$i]), "http") !== 0) continue; // это мы добавили echo $fp[$i]."<br>"; 

    either the second method, instead of the added line, we add to the same place

     if (filter_var($fp[$i], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === false) continue; 
    • Thanks a lot , but I ’m already delivering it 3 times, but we still haven’t read the PHP lectures, it's still HTML, and I have to hand over the lab - gvenog