Jump to content

Recommended Posts

How can I get an array from a text file that looks like

 

1:	10 Things You Need to Know About Losing Weight - 10 Things You Need to Know About Losing Weight, Signed, Factual,Science & Nature,Science & Technology,Sign Zone,TV, signed

Available:      Unknown
Categories:     Factual,Science & Nature,Science & Technology,Sign Zone,TV
Channel:        Signed
Desc:           Michael Mosley investigates the latest scientific breakthroughs in slimming.
Duration:       3600
Episode:        10 Things You Need to Know About Losing Weight
Firstbroadcast: signed: 2009-06-06T01:25:00+01:00 (4 days 12 hours ago) 
Index:          1
Lastbroadcast:  signed: 2009-06-06T01:25:00+01:00 (4 days 12 hours ago) 
Longname:       10 Things You Need to Know About Losing Weight
Modes:          signed: flashhigh3,flashlow1,flashlow2,flashnormal,flashvhigh2,iphone,n95_wifi,subtitles  
Modesizes:      signed: flashhigh3=350MB,flashlow1=352MB,flashlow2=352MB,flashnormal=225MB,flashvhigh2=659MB,iphone=227MB,n95_wifi=81MB 
Name:           10 Things You Need to Know About Losing Weight
Pid:            b00ksh7c
Thumbnail:      http://www.bbc.co.uk/iplayer/images/episode/b00ksh7c_150_84.jpg
Timeadded:      1244638700
Type:           tv
Versions:       signed
Web:            http://www.bbc.co.uk/programmes/b00ksh7c.html

2:	64 Zoo Lane - The Story of Herbert the Warthog, CBeebies, Children's,Entertainment & Comedy,Animation,TV, default

Available:      Unknown
Categories:     Children's,Entertainment & Comedy,Animation
Channel:        CBeebies
Desc:           Cartoon series about a girl who lives next door to a zoo. Lucy meets Herbert the Warthog, who tells her about his discovery of music on the savanna.
Duration:       660
Episode:        The Story of Herbert the Warthog
Expiry:         2009-06-10T17:39:00Z
Expiryrel:      in 0 days 4 hours
Firstbroadcast: default: 2007-09-14T09:00:00+01:00 (1 years 270 days 5 hours ago) 
Index:          2
Lastbroadcast:  default: 2009-06-03T18:25:00+01:00 (6 days 19 hours ago) 
Longname:       64 Zoo Lane
Modes:          default: flashhigh3,flashlow1,flashlow2,flashnormal,flashvhigh2,iphone,n95_wifi,subtitles  
Modesizes:      default: flashhigh3=64MB,flashlow1=64MB,flashlow2=64MB,flashnormal=41MB,flashvhigh2=121MB,iphone=42MB,n95_wifi=15MB 
Name:           64 Zoo Lane
Pid:            b00785fr
Player:         http://www.bbc.co.uk/iplayer/episode/b00785fr/64_Zoo_Lane_The_Story_of_Herbert_the_Warthog/?src=a_syn31
Thumbnail:      http://node1.bbcimg.co.uk/iplayer/images/episode/b00785fr_150_84.jpg
Timeadded:      1244638700
Type:           tv
Versions:       default
Web:            http://www.bbc.co.uk/programmes/b00785fr.html

 

this is an example as the files can contain more

 

how can I get the following to print into something like this

 

<?php
//loop controller
print "".$availiable." : ".$availiable_content."";
print "<br/>";
print "".$catagories." : ".$catagories_content."";
// SO ON 
?>

 

Is there any specific area to look at?

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/
Share on other sites

try something like this: (untested)

<?php
  $filename = 'test.txt';
  $data = array();
  foreach(file($filename) as $line){
    list($key,$value) = explode(':',$line,2);
    if(strlen(trim($key)))
      $data[strtolower(trim($key))] = trim($value);
  }
  print 'Available : ' . $data['available'];
?>

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/#findComment-853956
Share on other sites

<?php
  $filename = 'test.txt';
  $entries = array();
  $entry = null;
  foreach(file($filename) as $line){
    $parts = preg_split('/\s*:\s*/',trim($line),2);
    if(count($parts) == 2){
      if(is_numeric($parts[0])){ //New Entry
        if(is_array($entry))
          $entries[] = $entry;
        $entry = array();
      }elseif(is_array($entry))
        $entry[strtolower($parts[0])] = $parts[1];
    }
  }
  if(is_array($entry))
    $entries[] = $entry;
  
  foreach($entries as $entry){
    print "Available: {$entry['available']}<br>";
    print "Categories: {$entry['categories']}<br>";
    print "<br><br>";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/#findComment-853969
Share on other sites

ok...in that case, to save on resources, do it this way:

 

<?php
  $filename = 'test.txt';
  $entry = null;
  foreach(file($filename) as $line){
    //Split the data
    $parts = preg_split('/\s*:\s*/',trim($line),2);
    //Skip Blank Lines
    if(count($parts) !== 2) continue;
    
    if(is_numeric($parts[0])){ //New Entry
      if(is_array($entry) && count($entry))
        addEntry($entry);
      $entry = array();
    }elseif(is_array($entry))
      $entry[strtolower($parts[0])] = $parts[1];
  }
  if(is_array($entry) && count($entry))
    addEntry($entry);

  function addEntry ( $entry ) {
    //DB Insert Stuff here
    print "Available: {$entry['available']}<br>";
    print "Categories: {$entry['categories']}<br>";
    print "<br><br>";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/#findComment-853990
Share on other sites

can you see anything wrong with this? I have tried everything and it will not insert I have double checked spelling and everything i execute this code. The database connection is above the top if .

<?php
if(mysql_connect($dbhost, $dbuser, $dbpass)){
	print "Mysql Connected <br/><br/><br/><br/>";
	}
else{
	print"Mysql Not Connected <br/><br/><br/><br/>";
    }
  
  mysql_select_db($dbname);
   $filename = 'info.txt';
  $entry = null;
  foreach(file($filename) as $line){
    //Split the data
    $parts = preg_split('/\s*:\s*/',trim($line),2);
    //Skip Blank Lines
    if(count($parts) !== 2) continue;
    
    if(is_numeric($parts[0])){ //New Entry
      if(is_array($entry) && count($entry))
        addEntry($entry);
      $entry = array();
    }elseif(is_array($entry))
      $entry[strtolower($parts[0])] = $parts[1];
  }
  if(is_array($entry) && count($entry))
    addEntry($entry);

  function addEntry ( $entry ) {
    $availiable = "{$entry['available']}";
    $catagories = "{$entry['categories']}";
$channel = "{$entry['channel']}";
$description = "{$entry['desc']}";
$duration = "{$entry['duration']}";
$episode = "{$entry['episode']}";
$expiry = "{$entry['expiry']}";
$epiry_relative = "{$entry['expiryrel']}";
$first_broadcast = "{$entry['firstbroadcast']}";
$index = "{$entry['index']}";
$last_broadcasted = "{$entry['lastbroadcast']}";
$title_long = "{$entry['longname']}";
$mode = "{$entry['modes']}";
$mode_size = "{$entry['modesizes']}";
$title_name = "{$entry['name']}";
$player_url = "{$entry['player']}";
$pid = "{$entry['pid']}";
$thumbnail = "{$entry['thumbnail']}";
$time_added = "{$entry['timeadded']}";
$type = "{$entry['type']}";
$version = "{$entry['versions']}";
$ultimate_url = "{$entry['web']}";
   
if(mysql_query("INSERT INTO programme (availiable, 
									   catagories, 
									   channel, 
									   description, 
									   duration,										  
									   episode, 
									   expiry, 
									   expiry_relative, 
									   first_broadcast, 
									   index,
									   last_broadcasted, 
									   title_long, 
									   mode, 
									   mode_size, 
									   title_name, 
									   player_url, 
									   pid, 
									   thumbnail, 
									   time_added, 
									   type, 
									   version, 
									   ultimate_url) VALUES ('$availiable',
									   '$catagories',
									   '$channel',
									   '$description',
									   '$duration',
									   '$episode',
									   '$expiry',
									   '$expiry_relative',
									   '$first_broadcast',
									   '$index',
									   '$last_broadcasted',
									   '$title_long',
									   '$mode',
									   '$mode_size',
									   '$title_name',
									   '$player_url',
									   '$pid',
									   '$thumbnail',
									   '$time_added', 
									   '$type',
									   '$version',
									   '$ultimate_url',
									   ")){
	   print "Success";
   }
   else{
	   print "Error Not Entered";
   }

  }
?>

 

 

this is the output i get

 

Mysql Connected <br/><br/><br/><br/><br />
<b>Notice</b>:  Undefined index:  expiry in <b>G:\queue\parse.php</b> on line <b>40</b><br />
<br />
<b>Notice</b>:  Undefined index:  expiryrel in <b>G:\queue\parse.php</b> on line <b>41</b><br />

<br />
<b>Notice</b>:  Undefined index:  player in <b>G:\queue\parse.php</b> on line <b>49</b><br />
<br />
<b>Notice</b>:  Undefined variable: expiry_relative in <b>G:\queue\parse.php</b> on line <b>86</b><br />
Error Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not EnteredError Not Entered<br />

<b>Notice</b>:  Undefined index:  expiry in <b>G:\queue\parse.php</b> on line <b>40</b><br />
<br />
<b>Notice</b>:  Undefined index:  expiryrel in <b>G:\queue\parse.php</b> on line <b>41</b><br />
<br />
<b>Notice</b>:  Undefined index:  player in <b>G:\queue\parse.php</b> on line <b>49</b><br />

<br />
<b>Notice</b>:  Undefined variable: expiry_relative in <b>G:\queue\parse.php</b> on line <b>86</b><br />
Error Not Entered<br />
<b>Notice</b>:  Undefined index:  firstbroadcast in <b>G:\queue\parse.php</b> on line <b>42</b><br />

<br />
<b>Notice</b>:  Undefined index:  lastbroadcast in <b>G:\queue\parse.php</b> on line <b>44</b><br />
<br />
<b>Notice</b>:  Undefined index:  longname in <b>G:\queue\parse.php</b> on line <b>45</b><br />
<br />

<b>Notice</b>:  Undefined index:  modes in <b>G:\queue\parse.php</b> on line <b>46</b><br />
<br />
<b>Notice</b>:  Undefined index:  modesizes in <b>G:\queue\parse.php</b> on line <b>47</b><br />
<br />
<b>Notice</b>:  Undefined index:  versions in <b>G:\queue\parse.php</b> on line <b>54</b><br />

<br />
<b>Notice</b>:  Undefined variable: expiry_relative in <b>G:\queue\parse.php</b> on line <b>86</b><br />
Error Not Entered<br />
<b>Notice</b>:  Undefined index:  expiry in <b>G:\queue\parse.php</b> on line <b>40</b><br />

<br />
<b>Notice</b>:  Undefined index:  expiryrel in <b>G:\queue\parse.php</b> on line <b>41</b><br />
<br />
<b>Notice</b>:  Undefined index:  player in <b>G:\queue\parse.php</b> on line <b>49</b><br />
<br />

<b>Notice</b>:  Undefined variable: expiry_relative in <b>G:\queue\parse.php</b> on line <b>86</b><br />
Error Not Entered<br />
<b>Notice</b>:  Undefined index:  expiry in <b>G:\queue\parse.php</b> on line <b>40</b><br />
<br />

<b>Notice</b>:  Undefined index:  expiryrel in <b>G:\queue\parse.php</b> on line <b>41</b><br />
<br />
<b>Notice</b>:  Undefined index:  player in <b>G:\queue\parse.php</b> on line <b>49</b><br />
<br />

 

its a large file i am trying to process and have even extended the execte time in php ini sorry to be such a pain

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/#findComment-854093
Share on other sites

first, the notices are caused by entries that don't have those keys...you can hide them by putting this at the top of your code:

error_reporting(E_ALL  & ~E_NOTICE);

 

as far as not inserting into the DB...what is the mysql error? you can get it by changing:

print "Error Not Entered";

to

die("Error Not Entered: ".mysql_error());

 

my guess is one of your column names is a mysql reserved word:

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

if that is so, i would change the column name. but you can also use backticks in your query:

   if(mysql_query("INSERT INTO programme (`availiable`, 
                                 `catagories`, 
                                 `channel`, 
                                 `description`,

 

or, one of your values has an apostrophe in it...you can fix that by running all your values through mysql_real_escape_string() when building the entry. replace this:

      $entry[strtolower($parts[0])] = $parts[1];

with

      $entry[strtolower($parts[0])] = mysql_real_escape_string($parts[1]);

Link to comment
https://forums.phpfreaks.com/topic/161855-array-from-txt-file/#findComment-854230
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.