Hi,
I want to know how to replace variables of RTF file using PHP language.
Please help me....
Hi,
Try get the RTF data , and apply eval().
Something like this:
$rtfdata = file_get_contents('file.rtf');
eval("\$rtfdata = \"$rtfdata\";");
echo $rtfdata;
You need to be fat more specific with your needs. Are you talking about replacing RTF markup with html?
I have HotDocs created document where variables like this.
Name: «demo_Last name» «demo_First name»
DOB: «demo_DOB»
SSN: «demo_SSN»
Country: «Country Name» (Variables may include spaces.)
And also conditions like this:
«IF XYZ = "Some String"»
DRE I«ELSE IF XYZ = "Some other string"» DRE II
«ELSE IF XYZ = "else string"»DRE III«END IF»
«IF PQR = "1"» DRE I «ELSE IF PQR = "2"»DRE IV«ELSE IF PQR = "2" AND XYZ = "Some String"» Response«END IF»
Do I have to create something like compiler?
So you want to replace code blocks and variables/placeholders with php/html code? You may need some heavy preg_replace
use!
What about conditions??
And what if there are two variables in a condition?
Like
«ELSE IF PQR = "2" AND XYZ = "Some String"»
I really, really don't udnerstand what you're trying to achieve. The hotdoc is a template, specifically produced to work with a particular parser. Why do you need to change it to php?
echo <<<FOOBAR
Name: $lastname $firstname
DOB: $dob
SSN: $demo_SSN
Country: $Country_Name (Variables may include spaces.)
FOOBAR;
if($XYZ == "Some String"){
echo "DRE I";
}elseif($XYZ == "Some other string"){
echo "DRE II";
}elseif($XYZ == "else string"){
echo "DRE III";
}
if($PQR == "1"){
echo "DRE I";
}elseif($PQR == "2"){
echo "DRE IV";
}elseif($PQR == "2" && $XYZ == "Some String"){
echo "Response";
}
SOmething like that?
I can not change.
What I really want to do is that. I have RTF file. On the other hand I have an array of values to replace.
Thay way I want to achieve this goal is to open the file with fopen, and then replace variables with values, and also parse condition to show response.
Ok, in that case, your best bet would be to convert the document to a php file and use that as an include file. Either that of use a proper php templating engine. There are many to choose from, the most popular being Smarty, RainTPL and the more recent Twig (which is REALLY cool).
For example, a twig html document would look like this:
Name: {{lastname}} {{$firstname}}
DOB: {{dob}}
SSN: {{demo_SSN}}
Country: {{Country_Name}} (Variables may include spaces.)
{% if XYZ == "Some String" %}
<p>DRE I</p>
{% elseif kenny.dead %}
<p>DRE II</p>
{% else %}
<p>DRE III</p>
{% endif %}
{% if PQR == "1" %}
<p>DRE I</p>
{% elseif PQR == "2" %}
<p>DRE IV</p>
{% elseif PQR == "2" && XYZ == "Some String" %}
<p>Response</p>
{% endif %}
You could do something like this for the variables, but the conditional need some heavy duty manipulation:
<?php
$f = file_get_contents("txt.txt");
$array_values=array("lastname"=>"Gilbert", "firstname"=>"Rhod", "dob"=>"10/12/1976", "SSN"=> "64916541541NJ", "country_name"=> "Wales");
$vars =array("lastname"=> "demo_Last name", "firstname"=> "demo_First name", "dob"=>"demo_DOB", "SSN"=>"demo_SSN", "country_name"=> "Country Name");
$patternedvars = array_map('patternMe',$vars);
function patternMe($item){
$item = '/«' . $item . '»/';
return $item;
}
$f = nl2br(preg_replace($patternedvars,$array_values,$f));
echo $f;
?>
:-(
My problem is I cannot modify RTF file as there are many of them, and these file are using for other purposes too.
The only way I have to parse the file is using charrsid8996477 as it the only word which HotDocs is using as its start and end tags like this.
{\rtlch\fcs1 \af39\afs22 \ltrch\fcs0
\f39\fs22\cf2\insrsid3348527\charrsid8996477 demo_DOB}
And also I could not find ending tag. Starting charrsid8996477 is given and it is ending with "}"
How many are you talking about?
I can't see an easy way around this. Replacing just the if blocks could become a nightmare. It could also be the case where if conditions are a little more complex in some templates, so that your replacement fails.
A few hours converting these files manually may be quicker than writing a fance preg_replace routine that is likely to fail anyway.
There are about 30 files. And as I said these files are being used in other ways / other system. So modification of these files will create duplication, and when a single word in a file need to change, We will have to make these changes in my PHP files too.
To resolve this issue I wanted to use files as these files are provided to me.
I really can't see a way of producing a 'duplicate' php file from these types of template file without writing a hefty custom parser.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.