Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2004, 04:42 PM
WebProWorld New Member
 

Join Date: Nov 2003
Posts: 5
Lionelandre RepRank 0
Default looking for PHP properties file resources

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
Reply With Quote
  #2 (permalink)  
Old 03-21-2004, 05:25 PM
WebProWorld Member
 

Join Date: Jul 2003
Location: Toronto, Canada
Posts: 83
jamal RepRank 0
Default

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
Reply With Quote
  #3 (permalink)  
Old 03-21-2004, 06:02 PM
WebProWorld New Member
 

Join Date: Nov 2003
Posts: 5
Lionelandre RepRank 0
Default

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
Reply With Quote
  #4 (permalink)  
Old 03-21-2004, 06:25 PM
WebProWorld Member
 

Join Date: Jul 2003
Location: Toronto, Canada
Posts: 83
jamal RepRank 0
Default

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:
Code:
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:
Code:
<?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
Reply With Quote
  #5 (permalink)  
Old 03-22-2004, 08:42 AM
WebProWorld New Member
 

Join Date: Nov 2003
Posts: 5
Lionelandre RepRank 0
Default

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
Reply With Quote
  #6 (permalink)  
Old 03-22-2004, 02:41 PM
WebProWorld Member
 

Join Date: Jul 2003
Location: Toronto, Canada
Posts: 83
jamal RepRank 0
Default

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
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0