PDA

View Full Version : looking for PHP properties file resources



Lionelandre
03-21-2004, 03:42 PM
hello,

i'm looking for a resource that would guide me towards recomendations on how to use properties files in PHP.

I looked around, but couldn't find anything, and my O'reilly book (web database applications with PHP / mySQL) doesn't cover it!:(

Thanks a lot!

Lionel

jamal
03-21-2004, 04:25 PM
Hi Lionelandre;
when you say 'properties' files, what exactly do you mean? What purpose does a properties file(s) serve.

I did not quite understand your question, but I will be happy to help you if you try to rephrase (or maybe I'm so behind I don't know what you're talking about? :) ).

The only things I can think of right now are:
1) a 'config' file that configures settings for a PHP script.
2) php.ini which controls all PHP settings/properties on a server.
3) a function that return's a file's properties?

Reply, and I will reply back!
Yours,
Jamal

Lionelandre
03-21-2004, 05:02 PM
Jamal,

I'm looking for ways to have all my text messages (such as error messages, all the way to oage headers and titles) in a separate, external file.

This file stores an ID, with the text string next to it, like so:

1 = "welcome to acme.org"
2 = "Your username is empty, please try again."

and the list goes on.

in my PHP pages, I'd like to reference this text and have it displayed in line on the page.

Simple, really, eh ?

You can also use this to drive internationalization, if you have, say French.properties and English.properties, you swithch the files as the user clicks on the little flag.

HTH.

:L

jamal
03-21-2004, 05:25 PM
Ah! This time its clear.
Ok, what you have to do is make seperate files for each language or set of properties like you already mentioned. Now what I want to tell you is that you do not need this syntax:
1 = "this is the first message"
2 = "this is the second message"
in fact, you don't need the ID at all. Heres is why: lets suppose we have a file (text.properties) with the following content:


This is the first line of the file
This is the second line of the file
This is the third line of the file


In your PHP script this is what you would do:


<?php
$foo = file("text.properties");
?>


The above will take the contents of the file text.properties and put it into an array, therefore:
$foo[0] = "This is the first line of the file"
$foo[1] = "This is the second line of the file"
$foo[2] = "This is the third line of the file"

Note that it starts at 0, this is the way PHP is built, just like most languages out there. Meaning the first line will have an ID of 0 not 1. If this confuses you, then just leave the first line of the file blank, and simply dont use the variable $foo[0].

I use this method all the time, and it works great. Use caution however if you allow users to input data into your file (text.properties), if they press enter/return it will insert a new line character (\n) and split the text amongst 2 lines, or more. The solution is simple however:
$textToInput = str_replace("\n", "", $_POST[formText]);

Hope this is helpful, and that you understand. I will be more than happy to explain something if there is confusion.

Yours,
Jamal

Lionelandre
03-22-2004, 07:42 AM
Jamal,

yeah, this makes complete sense, thanks a lot for that ! :)

Is there a way, though, to use a specific ID for the message ? my issue with the approach you are suggesting is inserting a line inside the properties file will wreak havoc across the app !!!

Put it another way .... I'm going to hand the properties file to Marketing people............ 'nuf said?

Cheers,

:L

jamal
03-22-2004, 01:41 PM
Yes, sometimes I use commas in my files, and explode them. Here's what I mean:
Lets say $myvar = "1234,This is a sentence.";
then is you explode it like so

//Explode at comma (,)
$myarray = explode(",", $myvar);

Then $myarray[0] = "1234" and $myarray[1] = "This is a sentence."

Now the only thing with this is that you cannot have commas in the sentence, or else the array will be bigger. What I usually do is enter a set of characters that is unlikely to be in the sentence itself, such as '[,]'

like this $myvar = "1234[,]This is a sentence"
then when you explode, explode at "[,]" instead of ",".

You can implement it into a file and make it like a flat-file database. The file would have an ID, seperated by a string, and a sentence like so:

1234[,]This is the first line
3245[,]This is another line
4839[,]This is the last line