I need to practically generate a .PHP file using a PHP script or PHP command. The file generate will contain the following code:
<?php
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$lang_en="http://translate.google.com/translate_tts?tl=en&q={$text}";
$lang_ro="http://translate.google.com/translate_tts?tl=ro&q={$text}";
$lang_fr="http://translate.google.com/translate_tts?tl=fr&q={$text}";
$language=$lang_en;
$this->mp3data = file_get_contents($language);
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$en="http://translate.google.com/translate_tts?tl=en&q={$text}";
$ro="http://translate.google.com/translate_tts?tl=ro&q={$text}";
$fr="http://translate.google.com/translate_tts?tl=fr&q={$text}";
$lang=$en;
$this->mp3data = file_get_contents($lang);
return $this->mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
I want to do this because I want to have multiple choices regarding the language:
$language=$lang_en;
and
$lang=$en;
Not sure what your question is here. Generating a PHP file is no different than generating any other kind of file. Build a string of the source code you want to write, then a simple
file_put_contents('filename.php', $contents);will do the trick.Edit: Where are
$enand$lang_encoming from? Also, could these not be passed as parameters to the function? Why must they be hard-coded?You may also want to read up on heredocs for large blocks of text, but unfortunately those will replace your variables unless you escape them all with a backslash.