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
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>';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.