March 30, 2012

read name and value from every define('NAME','VALUE'); inside a .php file

Question by elcodedocle

I’m implementing a php interface to process a .php file containing a bunch of define sentences defining constants with text values to translate by somebody.

The input is parsed sentence by sentence and shown in the interface to the translator who will input the translation in a html textarea and send it to the server

By the end of the process an output file would be generated, identical to the input .php file, only with the define values translated.

E.g. processing an input file ‘eng.php’ like this:

<?php
define ("SENTENCE_1", "A sentence to translate");
?>

would give the translated output file ‘spa.php’ like this:

<?php
define ("SENTENCE_1", "The same sentence translated to spanish");
?>

For this I would like to know:

1) What is the best way to parse the input file to get the constant names and values in an array? (Something like $var[0][‘name’] would be “SENTENCE_1” and $var[0][‘value’] would be “A sentence to translate”)

2) Would it be possible to get the translation from google translator shown in the input textarea as a suggestion to edit for the person who is translating it? How? (I read google translator api v1 is no longer available and v2 is only available as a paid service. Are there any free alternatives?)

Answer by Starx

Use get_defined_constants() to get the list of all the defined constants.

To get userdefined constant specially

$allconstants = get_defined_constants(true);
print_r($allconstants['user']);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!