Re: [PHP] find and replace in php
Perfect Thankyou Henry "Erik Price" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > On Tuesday, July 2, 2002, at 01:52 PM, Henry wrote: > > > Imagine I have a piece of text > > > > "Dear [firstname] > > > > Thankyou for purchasing [product] on [date]. > > > > We're sure you'll be very [expectation]. > > > > Ta Very much." > > > > Whats the easiest way to replace all the things in square brackets with > > variables of the same name. > > > > i.e. $firstname, $product, $date, $expectation > > > > Whilst ensuring that if the variable doesn't exist then it will be > > replaced > > with "" (absolutely nothing). > > Here, I modified the template class I use for my site (very simple) to > do what you want. You need to have a template file that looks exactly > like what you wrote above (EXACTLY), nothing more. Here is a usage > example: > > // the following vars have already been defined in your application, > // or are set to empty strings as appropriate: > // $firstname, $product, $date, $expectation > > // create a new Template object > $template = new Template(); > > // specify where our template file is > $template->identifyTemplate('/path/to/file/above'); > > // set the parameters > $template->setParameter('firstname', $firstname); > $template->setParameter('product', $product); > $template->setParameter('date', $date); > $template->setParameter('expectation', $expectation); > > // generate the output string and store in $outputStr > $outputStr = $template->createOutput(); > // free memory occupied by the Template object instance > unset($template); > > // print our data > print($outputStr); > > Here is the class definition: > > > > class Template > { > /** > * @var template > */ > var $template; > /** > * @var finalOutput > */ > var $finalOutput; > /** > * @var parameters > */ > var $parameters = array(); > > /** > * identifyTemplate > * > * determine which file to use for a template > * > * @access public > * @param string $template path to a template file > */ > function identifyTemplate($template) > { > $this->template = $template; > } > > /** > * setParameter > * > * specify a name and a value to swap with that name > * > * this method can be called as many times as you have > * parameters. > * > * @access public > * @param string $name a parameter name > * @param string $value value of the parameter > */ > function setParameter($variable, $value) > { > $this->parameters[$variable] = $value; > } > > /** > * createPage > * > * substitute parameter values where parameter names are found > * > * @access public > * @return string template combined with values (transformed) > */ > function createOutput() > { > // read the template into an array, then > // generate a string from that array > $this->finalOutput = implode("", (file($this->template))); > > // loop though all the parameters, and > // set the variables from the file to > // their corresponding values > foreach ($this->parameters as $key => $value) { > $template_name = '[' . $key . ']'; > $this->finalOutput = str_replace($template_name, $value, > $this->finalOutput); > } > return $this->finalOutput; > } > } > > > > > > > Erik Price > Web Developer Temp > Media Lab, H.H. Brown > [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] find and replace in php
On Tuesday, July 2, 2002, at 01:52 PM, Henry wrote: > Imagine I have a piece of text > > "Dear [firstname] > > Thankyou for purchasing [product] on [date]. > > We're sure you'll be very [expectation]. > > Ta Very much." > > Whats the easiest way to replace all the things in square brackets with > variables of the same name. > > i.e. $firstname, $product, $date, $expectation > > Whilst ensuring that if the variable doesn't exist then it will be > replaced > with "" (absolutely nothing). Here, I modified the template class I use for my site (very simple) to do what you want. You need to have a template file that looks exactly like what you wrote above (EXACTLY), nothing more. Here is a usage example: identifyTemplate('/path/to/file/above'); // set the parameters $template->setParameter('firstname', $firstname); $template->setParameter('product', $product); $template->setParameter('date', $date); $template->setParameter('expectation', $expectation); // generate the output string and store in $outputStr $outputStr = $template->createOutput(); // free memory occupied by the Template object instance unset($template); // print our data print($outputStr); Here is the class definition: class Template { /** * @var template */ var $template; /** * @var finalOutput */ var $finalOutput; /** * @var parameters */ var $parameters = array(); /** * identifyTemplate * * determine which file to use for a template * * @access public * @param string $template path to a template file */ function identifyTemplate($template) { $this->template = $template; } /** * setParameter * * specify a name and a value to swap with that name * * this method can be called as many times as you have * parameters. * * @access public * @param string $name a parameter name * @param string $value value of the parameter */ function setParameter($variable, $value) { $this->parameters[$variable] = $value; } /** * createPage * * substitute parameter values where parameter names are found * * @access public * @return string template combined with values (transformed) */ function createOutput() { // read the template into an array, then // generate a string from that array $this->finalOutput = implode("", (file($this->template))); // loop though all the parameters, and // set the variables from the file to // their corresponding values foreach ($this->parameters as $key => $value) { $template_name = '[' . $key . ']'; $this->finalOutput = str_replace($template_name, $value, $this->finalOutput); } return $this->finalOutput; } } Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] find and replace in php
Nothing will be inserted if the variable is empty, so you could just use $firstname, $product, $date, etc. inside your "letter". The main problem is adding logic to your "letter" to respond appropriately based on the value of different variables. For example: $MailBody = ""; if (empty($firstname)) $MailBody .= "Dear Customer"; else $MailBody .= "Dear $firstname"; Note the use of .= to append to the string. -Original Message- From: Henry [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 02, 2002 10:53 AM To: [EMAIL PROTECTED] Subject: [PHP] find and replace in php Imagine I have a piece of text "Dear [firstname] Thankyou for purchasing [product] on [date]. We're sure you'll be very [expectation]. Ta Very much." Whats the easiest way to replace all the things in square brackets with variables of the same name. i.e. $firstname, $product, $date, $expectation Whilst ensuring that if the variable doesn't exist then it will be replaced with "" (absolutely nothing). Thankyou in advance Henry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This message is intended for the sole use of the individual and entity to whom it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If you are not the intended addressee, nor authorized to receive for the intended addressee, you are hereby notified that you may not use, copy, disclose or distribute to anyone the message or any information contained in the message. If you have received this message in error, please immediately advise the sender by reply email and delete the message. Thank you very much. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php