PHP variable variables array problem

gaptrast

Member
Messages
123
Reaction score
0
Points
16
Hello,

I have a problem with my flat file database...

they are built like this:

Code:
title:BLALBL
brief:Cool thing
thing:asdf
image[0]:http://....
image[1]:http://....

With the code I want the strings before the : shall be variable names and the string after shall be the value. THen I can use it like this:

PHP:
<?php

echo $title;
echo "<p>$brief</p>";
echo"<img src='$image[0]' />";
echo"<img src='$image[1]' />";
?>

Do you understand?? Here is my code that is nearly working::

PHP:
<?php
$file = file("../../txt/datafile.txt");


foreach ($file as $line_num => $line){

${"t".$line_num}=explode(":",$file[$line_num]);
// creating arrays ($t0,$t1,$t2 ...)

$${"t".$line_num}[0] = ${"t".$line_num}[1];

?>

The only problem is that the $image array (or any other array) will not work!! I can figure out what I do wrong. Thanks for any help:)
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
Hi gaptrast,

Try limiting “explode” to 2 substrings.

Code:
... = explode(":",$file[$line_num], [B][COLOR="red"]2[/COLOR][/B]);

In your code, the colon :) ) after 'http' is also a delimiter, so you are getting at least 3 strings instead of two back.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Rather than using a custom format, why not use something PHP already supports, such as ini,csv or a native PHP format, such as an array (whether stored as a PHP script or serialized)?
 

gaptrast

Member
Messages
123
Reaction score
0
Points
16
ok I have now changed to ini. but I have a problem:

Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, no and false results in "", yes and true results in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value​

Do I have to change all of theese to special html characters?? like on = &#111&#110
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Encoding them (whatever the scheme) is one option. However, an ampersand ('&') is one of the special characters, so you won't be able to use HTML entities.

Under PHP 5.2, you can enclose the value in double quotes (which you would need to do anyway if the string had any non-alphanumeric characters). Under 5.3, you'd use single quotes, or escape the characters with a backslash, or set the scanner mode to INI_SCANNER_RAW or (in some cases) do nothing: a dollar sign ('$'), for example, is only special when followed by a squiggly bracket ('{').

Another possibility is to use another format instead of PHP's configuration format, one where you won't have to escape or encode any characters, such as a PHP array stored in a script. If you don't care about having your data store be editable by text editors, you could use a Berkeley DB style database.

You left off closing semicolons in your sample HTML entities.
 
Top