Jump to content

Read from website/file


prudens

Recommended Posts

Hey,

 

 

      So let's say I have a file called "schools.txt":

 

University of Alaska
University of California
CalTech
University of Phoenix

 

How do I use pHp to read the file and store each line in an array Skool[]?

 

 

and say I have a webpage called "groups.html"

 

<html>
<head>
<title>
</head>
LINE 1
LINE 2
</html>

 

How do I skip the <html> tags and get straight to LINE1 and LINE2 and read and store in array?

 

 

Link to comment
https://forums.phpfreaks.com/topic/107843-read-from-websitefile/
Share on other sites

Probably have to read it into a sting, strip the HTML tags, explode by the new lines, then remove empty elements:

 

<?php
$file = file_get_contents('file.txt');
$file = strip_tags($file);
$lines = explode("\n",$file);
foreach($lines as $k=>$v){
    $v = trim($v);
    if(empty($v)){
        unset($lines[$k]);
    }
}
echo '<pre>'.print_r($lines,1).'<pre>';
?>

 

Actually, it would be easier to trim out the unnecessary white space first:

 

<?php
$file = file_get_contents('file.txt');
$file = trim(strip_tags($file));
$lines = explode("\n",$file);
echo '<pre>'.print_r($lines,1).'<pre>';
?>

Would be easier to store it in a database. It is, afterall, what databases are designed to be used for. Though a simple read of a text file is not difficult, things get harder when you wish to modify a school's name or delete it.

 

Or the requirements could change, and you may wish to store added information about each school.

 

A database is a more 'future proof' solution.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.