[Pro] Accessing Multi-dimensional arrays in a text file

Hi

I need to retrieve same specific data from a multi-dimensional array text file using a specific URL request using php

For the request has to be Website Domain Names, Online Stores & Hosting - Domain.com

The number being the record

The date.text file format is like this:

 1|data|data|data|data
 2|data|data|**load this 4th section into my $variable**|data
 3|data|data|data|data

How do I load this specific [3] bit into a php variable at the top of the page to use later?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You can parse a text file like this using the following loop (this reads through the entire file, so performance will drop as the file gets longer). I believe there are ways to get just a single line out of a text file without reading the whole thing, but I haven’t tried that myself. I mostly use this technique to get client-supplied CSV into a database.

$yourValue = '';
$theLineYouWant = 1; //2nd line in a zero-based array
$file = file_get_contents('/path/to/date.text');
$lines = preg_split('/rn|n/',$file,-1,PREG_SPLIT_NO_EMPTY);
foreach($lines as $key => $line){
    if($key == $theLineYouWant){
        $fields = explode('|',trim($line));
        $yourValue = ($line[3] || '');
    }
}

I wasn’t sure if the numbers in your example were there in the file or were just to indicate the format.

Also, I split the initial text file on rn or just n, which should take care of any Windows or Unix line-endings. The only thing I didn’t include here was Classic Mac line-endings, which I believe are simply r. Test to see what your text file does in this case, especially if you can’t control the source or format of this file.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Walt

The text file would grow over time so really don’t want to load it all. The numbers are created by the original script relating to each blog post.

I’m testing the PulsePro blog which is inserted halfway down the page, and would like to extract just the blog post title to push into the page head tag when the blog post link is clicked. I need my own routine to access the text file to process it before the Pulse script runs

See here: http://www.printlineadvertising.co.uk/blog/

Click a post heading for example using http://www.printlineadvertising.co.uk/blog/?d=3

Part of the work is done, Pulse at least the URL is pointing to the right part of the text file just need $d[3] ?

Have not been able to find anything on how to do this. Is it possible?

David

On 11 Jun 2010, at 14:33, waltd wrote:

You can parse a text file like this using the following loop (this reads through the entire file, so performance will drop as the file gets longer). I believe there are ways to get just a single line out of a text file without reading the whole thing, but I haven’t tried that myself. I mostly use this technique to get client-supplied CSV into a database.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I don’t see anything in the PHP manual that references a point inside
a file by line. There is fseek which moves the pointer to a specified
byte offset, and fread which reads until it reaches a specified
length, but I think we may be over-thinking this a bit. Try the code I
posted and compare its performance with a page where you don’t call
it. Premature optimization is the root of all evil, after all.

Walter

On Jun 11, 2010, at 10:15 AM, David Owen wrote:

Thanks Walt

The text file would grow over time so really don’t want to load it
all. The numbers are created by the original script relating to
each blog post.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter

Have I got this test right? I’m just returning: 1 (on line 1?)

http://www.printlineadvertising.co.uk/demo/blogtest/get-text.php

The text file is here: http://www.printlineadvertising.co.uk/demo/blogtest/text.txt

<?php

$yourValue = '';
$theLineYouWant = 1; //2nd line in a zero-based array
$file = file_get_contents('blogfile.txt');
$lines = preg_split('/rn|n/',$file,-1,PREG_SPLIT_NO_EMPTY);
foreach($lines as $key => $line){
   if($key == $theLineYouWant){
       $fields = explode('|',trim($line));
       $yourValue = ($line[3] || '');
   }
}

?>



<?php print "$yourValue" ; ?>

David

On 11 Jun 2010, at 15:33, Walter Lee Davis wrote:

I don’t see anything in the PHP manual that references a point inside a file by line. There is fseek which moves the pointer to a specified byte offset, and fread which reads until it reaches a specified length, but I think we may be over-thinking this a bit. Try the code I posted and compare its performance with a page where you don’t call it. Premature optimization is the root of all evil, after all.

Walter

On Jun 11, 2010, at 10:15 AM, David Owen wrote:

Thanks Walt

The text file would grow over time so really don’t want to load it all. The numbers are created by the original script relating to each blog post.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I think what’s happening here is it’s returning true, and print() is
converting that to 1. I over-optimized my code where I assigned the
value. (There’s that darned problem again.) Change it to this:

   $yourValue = ($line[3]) ? $line[3] : '';

What I wrote would work in JavaScript or Ruby, but not PHP.

Walter

On Jun 11, 2010, at 11:23 AM, David Owen wrote:

Walter

Have I got this test right? I’m just returning: 1 (on line 1?)

http://www.printlineadvertising.co.uk/demo/blogtest/get-text.php

The text file is here: http://www.printlineadvertising.co.uk/demo/blogtest/text.txt

<?php

$yourValue = '';
$theLineYouWant = 1; //2nd line in a zero-based array
$file = file_get_contents('blogfile.txt');
$lines = preg_split('/rn|n/',$file,-1,PREG_SPLIT_NO_EMPTY);
foreach($lines as $key => $line){
  if($key == $theLineYouWant){
      $fields = explode('|',trim($line));
      $yourValue = ($line[3] || '');
  }
}

?>



<?php print "$yourValue" ; ?>

David

On 11 Jun 2010, at 15:33, Walter Lee Davis wrote:

I don’t see anything in the PHP manual that references a point
inside a file by line. There is fseek which moves the pointer to a
specified byte offset, and fread which reads until it reaches a
specified length, but I think we may be over-thinking this a bit.
Try the code I posted and compare its performance with a page where
you don’t call it. Premature optimization is the root of all evil,
after all.

Walter

On Jun 11, 2010, at 10:15 AM, David Owen wrote:

Thanks Walt

The text file would grow over time so really don’t want to load it
all. The numbers are created by the original script relating to
each blog post.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

And one more catch:

    $yourValue = ($fields[3]) ? $fields[3] : '';

Not $line[3];

Walter

On Jun 11, 2010, at 11:35 AM, Walter Lee Davis wrote:

I think what’s happening here is it’s returning true, and print() is
converting that to 1. I over-optimized my code where I assigned the
value. (There’s that darned problem again.) Change it to this:

 $yourValue = ($line[3]) ? $line[3] : '';

What I wrote would work in JavaScript or Ruby, but not PHP.

Walter

On Jun 11, 2010, at 11:23 AM, David Owen wrote:

Walter

Have I got this test right? I’m just returning: 1 (on line 1?)

http://www.printlineadvertising.co.uk/demo/blogtest/get-text.php

The text file is here: http://www.printlineadvertising.co.uk/demo/blogtest/text.txt

<?php

$yourValue = '';
$theLineYouWant = 1; //2nd line in a zero-based array
$file = file_get_contents('blogfile.txt');
$lines = preg_split('/rn|n/',$file,-1,PREG_SPLIT_NO_EMPTY);
foreach($lines as $key => $line){
 if($key == $theLineYouWant){
     $fields = explode('|',trim($line));
     $yourValue = ($line[3] || '');
 }
}

?>



<?php print "$yourValue" ; ?>

David

On 11 Jun 2010, at 15:33, Walter Lee Davis wrote:

I don’t see anything in the PHP manual that references a point
inside a file by line. There is fseek which moves the pointer to a
specified byte offset, and fread which reads until it reaches a
specified length, but I think we may be over-thinking this a bit.
Try the code I posted and compare its performance with a page
where you don’t call it. Premature optimization is the root of all
evil, after all.

Walter

On Jun 11, 2010, at 10:15 AM, David Owen wrote:

Thanks Walt

The text file would grow over time so really don’t want to load
it all. The numbers are created by the original script relating
to each blog post.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

That’s caught it

http://www.printlineadvertising.co.uk/demo/blogtest/get-text.php

Think I can plug in the rest to get up and running.

Thanks again.

David

On 11 Jun 2010, at 16:44, Walter Lee Davis wrote:

$yourValue = ($fields[3]) ? $fields[3] : ‘’;


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

http://scripty.walterdavisstudio.com/remote.php

Walter

On Jun 11, 2010, at 11:44 AM, Walter Lee Davis wrote:

And one more catch:

  $yourValue = ($fields[3]) ? $fields[3] : '';

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’m not sure if this matters to you in this case, but it’s worth
pointing out to anyone else using this CMS: I was able to access this
data store from my server simply because I knew the URL.

 $file = file_get_contents('http://www.printlineadvertising.co.uk/demo/blogtest/text.txt') 

;

If you use this technique to do anything meaningful or requiring
security, you might want to use an htaccess rule to deny all requests
for this file from outside the server. This goes double for any
address books or other systems that store e-mail addresses. A treasure
trove awaits the first person who can guess the path to your data
file. At the very least, name it something unguessable.

Walter

On Jun 11, 2010, at 11:49 AM, David Owen wrote:

That’s caught it

http://www.printlineadvertising.co.uk/demo/blogtest/get-text.php

Think I can plug in the rest to get up and running.

Thanks again.

David

On 11 Jun 2010, at 16:44, Walter Lee Davis wrote:

$yourValue = ($fields[3]) ? $fields[3] : ‘’;


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

That was just the demo file ~ now gone. Yes its a problem with these set format text CMS, like webyep, there are certain things you can do to find the backend regardless if you know the layout.

David

On 11 Jun 2010, at 17:00, Walter Lee Davis wrote:

I’m not sure if this matters to you in this case, but it’s worth pointing out to anyone else using this CMS: I was able to access this data store from my server simply because I knew the URL.

$file = file_get_contents(‘http://www.printlineadvertising.co.uk/demo/blogtest/text.txt’);

If you use this technique to do anything meaningful or requiring security, you might want to use an htaccess rule to deny all requests for this file from outside the server. This goes double for any address books or other systems that store e-mail addresses. A treasure trove awaits the


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

It’s working now, with an exception to swap the title back to the default when not accessing a blog post direct ~ slight more search engine friendly

http://www.printlineadvertising.co.uk/blog/

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Anyone using Pulse might want to create a .htaccess file to protect
the blocks folder

<Files ~ ".txt$">
Order allow,deny
Deny from all
</Files>

put this in your blocks folder

David

On 11 Jun 2010, at 17:00, Walter Lee Davis email@hidden wrote:

you might want to use an htaccess rule to deny all requests for this
file from outside the server.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

At 10:33 -0400 11/6/10, Walter Lee Davis wrote:

I don’t see anything in the PHP manual that references a point
inside a file by line. There is fseek which moves the pointer to a
specified byte offset, and fread which reads until it reaches a
specified length, but I think we may be over-thinking this a bit.
Try the code I posted and compare its performance with a page where
you don’t call it. Premature optimization is the root of all evil,
after all.

Walter

On Jun 11, 2010, at 10:15 AM, David Owen wrote:

Thanks Walt

The text file would grow over time so really don’t want to load it
all. The numbers are created by the original script relating to
each blog post.

Instead of opening the file directly, you could use shell_exec to run
the Unix ‘tail’ command on the file and put the result in a variable
that you then process. ‘tail -n filename’ just outputs the last n
lines of filename. If you’ve any idea for a suitable n in could help
as the file gets larger. This assumes shell_exec is enabled on your
server. If not, ‘popen’ running ‘tail’ may be useable instead.

David


David Ledger - Freelance Unix Sysadmin in the UK.
HP-UX specialist of hpUG technical user group (www.hpug.org.uk)
email@hidden
www.ivdcs.co.uk


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options